Solved

Insert contentcontrol in bookmark, but only if bookmark has currently no contentcontrol

Posted on 2011-02-16
4
670 Views
Last Modified: 2012-05-11
Hi Experts,

I want to place a contentcontrol within ( an existing) bookmark, but only if there is currently no contentcontrol in the bookmark which already contains text. there might be normal text though in the bookmark which can we overwritten.

This is what I have so far:

Dim objcc As ContentControl
Dim abmr As Range

'This adds the contentcontrol to the bookmark. I want to use the tag to identify the contentcontrol later, and the placeholder text to check if it is empty or not:

 Set objCC = ActiveDocument.Bookmarks("bk_SectionXII").Range.ContentControls.Add_ wdContentControlRichText)
  objCC.Range.text = ""
  objCC.SetPlaceholderText text:="Copy Quality Agreement here"
  objCC.Tag = "CC_QualityAgreement"
  objCC.Range.Font.Color = wdColorRed

' this sets the bookmark back around the control, so that the macro can used repeatedly:
Set abmr = ActiveDocument.Bookmarks("bk_SectionXII").Range.Paragraphs(1).Range
abmr.Expand wdParagraph
abmr.Collapse wdCollapseStart
abmr.MoveEnd Unit:=wdParagraph, Count:=1
abmr.MoveEnd Unit:=wdCharacter, Count:=-1
ActiveDocument.Bookmarks.Add Name:="bk_SectionXII", Range:=abmr

Now, what I need is something which checks whether the contentcontrol with the tag "CC_QualityAgreement" exists, and whether it is empty or not. Empty is considered if the placeholder text "Copy Quality Agreement here" appears. Everything else is considered not empty.
If contentcontrol is there and if empty: do nothing
if contentcontrol is there but with (other than the placeholder) text: Ask if it should replaced with empty control; if yes, replace, if no, do nothing
If bookmark contains other text (anything else), overwrite and place contentcontrol within bookmark.

I tried with for each contentcontrol (objcc) in ActiveDocument.ContentControls
If objcc.tag = "CC_QualityAgreement" then
etc, but it don't lead me knowhere.

Help is highly appreciated
Ruth
0
Comment
Question by:Ruthie21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 34911041
I think that you would need something like this
Dim cc As ContentControl
    Dim bInsert As Boolean
    
    bInsert = True
    For Each cc In abmr.ContentControls
        If cc.Tag = "CC_QualityAgreement" Then
            If cc.Range.Text = "Copy Quality Agreement here" Then
                bInsert = (MsgBox("Replace?", vbYesNo) = vbYes)
            Else
                bInsert = False
            End If
            Exit For
        End If
    Next cc
    
    If bInsert Then
        ambr.Delete
        Set cc = abmr.ContentControls.Add(wdContentControlRichText)
    End If

Open in new window

0
 

Author Comment

by:Ruthie21
ID: 34911304
Hi

I tried it, but got an error message(object variable not set) in the second line:
For Each cc in abmr.Contentcontrols

Any idea why?

Thanks again
Ruth
0
 

Author Comment

by:Ruthie21
ID: 34911332
Oh I see..
0
 

Author Comment

by:Ruthie21
ID: 34911366
Works! Thanks a lot!
I just switched the msgbox to the case when the text of the contentcontrol has been changed.

Dim cc As ContentControl
    Dim bInsert As Boolean
   
    bInsert = True
    For Each cc In abmr.ContentControls
        If cc.Tag = "CC_QualityAgreement" Then
            If cc.Range.Text = "Copy Quality Agreement here" Then
                bInsert =  False      
                  Else
                bInsert = (MsgBox("Replace?", vbYesNo) = vbYes)
False
            End If
            Exit For
        End If
    Next cc
   
    If bInsert Then
        ambr.Delete
        Set cc = abmr.ContentControls.Add(wdContentControlRichText)
    End If
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Auto-merge multiple transaction ledgers in Excel 5 43
VB script help 23 49
MS Word and other office apps menu size 17 85
How to set PAUSE for this script ? 5 21
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question