?
Solved

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

Posted on 2011-02-16
4
Medium Priority
?
684 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses
Course of the Month15 days, left to enroll

840 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