Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-02-2018, 04:18 PM
madempress madempress is offline Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Windows 10 Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Office 2016
Novice
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure
 
Join Date: Mar 2018
Posts: 5
madempress is on a distinguished road
Talking 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.
Reply With Quote
  #2  
Old 03-13-2018, 11:23 AM
madempress madempress is offline Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Windows 10 Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Office 2016
Novice
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure
 
Join Date: Mar 2018
Posts: 5
madempress is on a distinguished road
Default

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)
I get more or less a loop of "Error 91: Object variable not set" and "Expected function or variable" if I try to define a range as the selection.wholestory or .ungroup the range (or selection, which I tried too). I vaguely understand why, I'm just not sure how to approach what to do here.

Still hoping for help~
Reply With Quote
  #3  
Old 03-13-2018, 02:14 PM
macropod's Avatar
macropod macropod is offline Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Windows 7 64bit Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #4  
Old 03-13-2018, 03:13 PM
madempress madempress is offline Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Windows 10 Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Office 2016
Novice
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure
 
Join Date: Mar 2018
Posts: 5
madempress is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 03-13-2018, 06:38 PM
macropod's Avatar
macropod macropod is offline Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Windows 7 64bit Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by madempress View Post
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.
True, but that doesn't mean you can't use 'read-only' protection with the appropriate ranges marked as exceptions. In any event, you could take the opportunity to enhance what you've been given with content controls or formfields (not both in the same document).
Quote:
Originally Posted by madempress View Post
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).
What you can do depends on what kind of editing restrictions you apply. Word's 'filling in forms' protection is more restrictive than its 'read-only' protection, but the latter doesn't work with formfields.
Quote:
Originally Posted by madempress View Post
What did you mean by rad-only protection? I'm not familiar with that term.
Typo - 'read-only' protection.

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]
Reply With Quote
  #6  
Old 03-14-2018, 10:04 AM
madempress madempress is offline Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Windows 10 Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Office 2016
Novice
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure
 
Join Date: Mar 2018
Posts: 5
madempress is on a distinguished road
Default

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
I have no idea how to go about re-grouping the document using this, since this doesn't use the selection.wholestory successfully, which is how I would theoretically target a .ContentControls.Add (wdContentControlGroup). But it is half my solution. After all, if the document can be ungrouped programatically, it can be grouped!
Attached Files
File Type: dotm TrackingForm_DRAFT.dotm (44.4 KB, 9 views)
Reply With Quote
  #7  
Old 03-14-2018, 11:25 AM
madempress madempress is offline Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Windows 10 Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Office 2016
Novice
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure
 
Join Date: Mar 2018
Posts: 5
madempress is on a distinguished road
Default

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)
Now I just have to figure out how to de-select everything at the end so it doesn't make users panic. =D
Reply With Quote
  #8  
Old 03-14-2018, 03:14 PM
macropod's Avatar
macropod macropod is offline Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Windows 7 64bit Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Tags
grouping, helpme, macro in word



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Word Document Review Procedure Suggestions FedSteve Word 1 09-01-2016 11:23 PM
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Grouping or ungrouping resets Wrapping style... why? addey Word 7 02-13-2015 05:22 AM
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure Pivot table grouping problem 2 tables need different grouping differentdrummer Excel 3 12-10-2013 01:19 AM
Ungrouping/grouping to protect document that deletes bookmark rng at contentcontrolonexit procedure 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft