View Single Post
 
Old 03-26-2022, 05:08 PM
skarden skarden is offline Windows Vista Office 2010 64bit
Novice
 
Join Date: Dec 2011
Location: Orlando, FL
Posts: 27
skarden is on a distinguished road
Default

I would change the code in one main way, and that is to test if the ContentControl ("CC") is locked. If it is, and you don't unlock it, then you won't be able to delete it.

It is also important to note that this will not work for a nested CC unless you unlock them too. You would need to add a test if there is one or more parents. The best way to do that is to call a Function that tests the a parent's ID. If there is no parent then it will error out. If there is, then unlock the Parent and pass the Parent CC (via the ID you just got) back to the function and keep going until you get to the top Parent Level (i.e. Parent_ID will be "" when the test for it fails).

Sub Copy_CC_Text

On Error Goto lblError

Dim bLocked As Boolean

Dim oCC As ContentControl

Dim oRng As Range

For Each oCC In ActiveDocument.ContentControls
If Selection.InRange(oCC.Range) Then
Set oRng = oCC.Range
If oCC.LockContentControl = True Then
bLocked = True
oCC.LockContentControl = False
End If
oCC.Delete
oRng.FormattedText.Copy
ActiveDocument.Undo 1
If bLocked Then
oCC.LockContentControl = True
End If
Exit For
End If
Next oCC

lblExit:
Exit Sub
lblError:
Debug.Print "In Copy_CC_Text got error: " & Err.Number & ", " & Err.Description
Goto lblExit
Exit Sub

Last edited by skarden; 03-27-2022 at 01:01 PM.
Reply With Quote