
03-02-2018, 04:18 PM
|
Novice
|
|
Join Date: Mar 2018
Posts: 5
|
|
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure
I am a bit new to the Alt+F11 part of MSWord. I cobbled together the following code from a lot of different forums and gmaxey and gmayor answers. Word version is 2016.
The following is the code I've put into a form that my office uses to track items for quality purposes. The form has several content controls, which I tend to put in tables so that the form also looks nice when it needs to be filled out by hand.
There's a Combo Box Content Control (Case "Purpose") that users use to say why the item is leaving the building. For most of the reasons items leave the building, users need a Rich Text Content Control in different part of the form (separate table) to write instructions, insert diagrams, etc. I have this as a building block "InstructElse" that I insert into the bookmark "BoxInstruct."
One reason, however, requires a separate set of instructions, which I use the code to insert in place of the Rich Text Content Control. The instructions are building block "InstructEoL".
Prior to needing these separate instructions (and any code), I had been using ctrl+A > Group (Developer > Controls > Group) to protect the form. I can't find any real guidance as to ungroup all content so that the bookmark range can be deleted and then grouping everything when the changes are done so that the user can't accidentally edit the form.
The code, which works absent any attempt to group/ungroup contents (leaving it unprotected):
Code:
Option Explicit
Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Select Case ContentControl.Title
Case "Purpose"
ActiveDocument.Fields.Update
Select Case ContentControl.Range.Text
Case "End of Life"
InsertBBinBM "BoxInstruct", "InstructEoL"
Case Else
InsertBBinBM "BoxInstruct", "InstructElse"
End Select
Case Else
'The user exited some other content control that we don't care about.
End Select
lbl_Exit:
Exit Sub
End Sub
Sub InsertBBinBM(ByRef BMTarget As String, BBName As String)
'The above calls out the sub-procedure asked for: InsertBBinBM, and the two properties...
'...it refers to (target bookmark, building block name).
Dim rng As Word.Range
Dim cc As ContentControl
'ERROR Unlock the document by ungrouping all objects.
'Set target range for the bookmark being inserted. This is the bookmark "BoxInstruct" range.
Set rng = ActiveDocument.Bookmarks(BMTarget).Range
'Unlock any content controls in the range so that it can be cleared! For Each statements have to per-variable, hence different variables every time for the same range.
'For Each statements have to have unique variables, so create a unique label for the content controls being unlocked.
Dim rccO As ContentControl
For Each rccO In ActiveDocument.ContentControls
rccO.LockContentControl = False
Next rccO
'Clear the target range to ensure the bookmark range is empty of previous content. This only works if the content controls are not locked!
rng.Delete
Application.ScreenUpdating = False
If Not BBName = " " Then
'Tell the range to insert the building block (gallery: Custom 1, Category "InstaForm"...
'...and then the name that you defined in your select case argument above.
Set rng = ActiveDocument.AttachedTemplate.BuildingBlockTypes(wdTypeCustom1).Categories("InstaForm").BuildingBlocks(BBName).Insert(rng, True)
Else
rng.Text = ""
End If
'Lock all content controls again.
Dim rccI As ContentControl
For Each rccI In ActiveDocument.ContentControls
rccI.LockContentControl = True
Next rccI
'Recreate the the bookmark.
ActiveDocument.Bookmarks.Add BMTarget, rng
'ERROR Re-group all objects to effectively lock the document from editing again.
Application.ScreenUpdating = True
End Sub
(Sorry about all the comments, documenting all code is required by our quality process and it helped me track what I was doing anyway).
The two places the comments are marked with ERROR are where I am pretty sure the solution needs to go. I've tried:
Code:
Dim sO As Shapes
For Each sO In ActiveDocument.Shapes
sO.Ungroup
Next sO
And:
Code:
ActiveDocument.Shapes.SelectAll
Shapes.Ungroup
And a few other variants of the two above, but no luck.
I don't think I would have this problem if I were using a building block gallery content control instead of a bookmark as my location of change, because the error comes from trying to delete the grouped range of the bookmark.
But having gotten this far, I'm really curious if it is possible and how to do it (I might need it later!). I hope someone can help.
|