Link to home
Start Free TrialLog in
Avatar of Ruthie21
Ruthie21Flag for United States of America

asked on

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

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
Avatar of GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ruthie21



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
Oh I see..
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      
                bInsert = (MsgBox("Replace?", vbYesNo) = vbYes)
            End If
            Exit For
        End If
    Next cc
    If bInsert Then
        Set cc = abmr.ContentControls.Add(wdContentControlRichText)
    End If