Assistance Working with Content Control and VBA

Hi Experts.

I'm working in a report template. Page 1 is a table containing title, page 2 is document control information contained in several tables.  I've made a bookmark "ContentControlArea" for the table on page 1 and the tables on page 2. I have then selected the bookmarked area "ContentControlArea" and manually apply content control by pressing the Developer Tab, Group > Group. Then I press the button Properties and tick "Content Control Cannot be deleted".

This works well manually, but then I need to take the content control off via VBA to perform some actions and then turn it back on. Taking it off seems to be okay, but I can't get it to turn back on. It says it is not a valid selection. See my code below (sub contentcontrollock)

Can someone assist?


Sub ContentControlUnlock()
    ActiveDocument.GoTo What:=wdGoToBookmark, Name:="ContentControlSection"
    Selection.ParentContentControl.LockContentControl = False   
    Selection.Range.ParentContentControl.Ungroup
End Sub

Sub ContentControlLock()
    ActiveDocument.GoTo What:=wdGoToBookmark, Name:="ContentControlSection"
    Selection.Range.ContentControls.Add (wdContentControlGroup)
    Selection.ParentContentControl.LockContentControl = True

End Sub

Open in new window

Fi69Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

irudykCommented:
Instead of using:
ActiveDocument.GoTo What:=wdGoToBookmark, Name:="ContentControlSection"
try:
Selection.GoTo What:=wdGoToBookmark, Name:="ContentControlSection"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fi69Author Commented:
Thank you, that helped. My code still needed amendment, but it seems to work now.
Sub ContentControlUnlock()

    Selection.GoTo What:=wdGoToBookmark, Name:="ContentControlSection"
 
    Dim objCC As ContentControl
    Set objCC = ActiveDocument.ContentControls(1)
    objCC.LockContentControl = False
    objCC.Ungroup
  
End Sub

Sub ContentControlLock()

    
    Selection.GoTo What:=wdGoToBookmark, Name:="ContentControlSection"
    Dim objCC As ContentControl
    
    Set objCC = Selection.ContentControls.Add(wdContentControlGroup)
    
    objCC.LockContentControl = True
    objCC.LockContents = True

   

End Sub

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.