Solved

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

Posted on 2011-02-16
4
651 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
  • 3
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Oh I see..
0
 

Author Comment

by:Ruthie21
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now