|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 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. |
#2
|
|||
|
|||
Update: A co-worker showed me how to record macros to try to uncover the mystery. Unfortunately, nothing that was recorded works (even as its own sub procedure). The document DOES start out with everything grouped, as intended.
Recorded macro: Code:
'Selecting all with ctrl+A Selection.WholeStory 'Hitting Developer > Controls > Ungroup Selection.Range.ParentContentControl.Ungroup 'Selecting all with ctrl+A Selection.WholeStory 'Developer > Controls > Group Selection.Range.ContentControls.Add (wdContentControlGroup) Still hoping for help~ |
#3
|
||||
|
||||
If users are only able to make inputs where you've provided content controls, why not skip all the grouping, etc. and simply apply 'filling in forms' protection. Alternatively, if there are other editable areas, you might use 'read-only' protection with the appropriate ranges marked as exceptions.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Thank you for your reply! Current document protections:
Forms are set to Read-Only so that they can be filled out and printed or saved as a unique record. They're a combination of instructional paragraphs and tables holding content controls. Users can't accidentally add text to the instructional paragraphs via grouping the entire document (i.e. ctrl+A > group). My predecessor did not use forms, just blank spaces in tables, so some of our forms cannot use filling in forms as an editing restriction. When I began updating, I looked into using Restricting Editing, and at some point discovered certain functions that I wanted did not work while it was active (I'd honestly have to go back, I can't remember specifically why). Grouping has worked well up until this point. I tried Restricting Editing to Filling in Forms and the same VBA error comes up in attempting to execute the range.delete, as though maybe "Filling in Forms" is an existing macro to group the entire document. What did you mean by rad-only protection? I'm not familiar with that term. |
#5
|
||||
|
||||
Quote:
Quote:
Quote:
It would be a lot easier to give applicable advice if we could see the actual document. Can you attach the document to a post with some representative content (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Attached is the form. I did make some headway (and found a bug in my initial form, w00!). The form should start out fully grouped. The only time new content is programmatically altered is when "Purpose for Release" is "End of Life".
The form now ungroups during contentcontrolonexit the way I want it to, using: Code:
For Each cc In ActiveDocument.ContentControls If cc.Type = wdContentControlGroup Then cc.Ungroup End If Next cc |
#7
|
|||
|
|||
Solved it!
Code:
'(Re)Group all objects to effectively lock the document from editing again. Dim ctrlA As Word.Range Dim ctrlAcc As ContentControl Set ctrlA = ActiveDocument.Range ctrlA.Select Set ctrlAcc = ActiveDocument.ContentControls.Add(wdContentControlGroup, ctrlA) |
#8
|
||||
|
||||
There is nothing about your document that indicates any need for all this grouping/ungrouping circumlocution; all you've ever needed was to apply the appropriate 'filling in forms' or 'read-only' (with exceptions) protection and temporarily disabling that protection while inserting the conditional content.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
grouping, helpme, macro in word |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word Document Review Procedure Suggestions | FedSteve | Word | 1 | 09-01-2016 11:23 PM |
Grouping or ungrouping resets Wrapping style... why? | addey | Word | 7 | 02-13-2015 05:22 AM |
Pivot table grouping problem 2 tables need different grouping | differentdrummer | Excel | 3 | 12-10-2013 01:19 AM |
How do I combine two different contentcontrolonexit macros? | jamles12 | Word VBA | 2 | 11-12-2013 06:51 PM |
Document selection procedure | kennethc | Word | 0 | 09-15-2010 02:56 PM |