?
Solved

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

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Suggested Courses

765 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