![]() |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
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 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 Code:
ActiveDocument.Shapes.SelectAll Shapes.Ungroup 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. ![]() |
Tags |
grouping, helpme, macro in word |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
FedSteve | Word | 1 | 09-01-2016 11:23 PM |
![]() |
addey | Word | 7 | 02-13-2015 05:22 AM |
![]() |
differentdrummer | Excel | 3 | 12-10-2013 01:19 AM |
![]() |
jamles12 | Word VBA | 2 | 11-12-2013 06:51 PM |
Document selection procedure | kennethc | Word | 0 | 09-15-2010 02:56 PM |