#1
|
|||
|
|||
Show or Hide Text using Mapped CCs and a CustomXMLPart Event Monitor
I got interested in another thread in this forum asking how to show/hide a table based on a check box change.
Years ago, I tinkered with the subject idea but gave up in frustration because Microsoft has never resolved several issues with the Document ContentControl events. Yesterday I rolled up my sleeves and took another stab at it. The issues with the events remains, but at least I have something, while not perfect, at least functional. I would be interested in any comments on the method and particularly any ideas to improve the work around. Thanks. Cross posted: https://www.msofficeforums.com/word-...-checkbox.html Last edited by gmaxey; 05-02-2024 at 04:38 AM. |
#2
|
||||
|
||||
Greg
I've had a big play with your code and wrestled with the same event not firing that you encountered. I tried a bunch of different things but couldn't figure out how to get the xml updates to stick without your document variable workaround. The only thing I would suggest that you don't need three nodes to switch contents around. I would toggle the mapping of the CC rather than changing the customXML. You still need the nullnode but only need one contentnode if changing the mapping on the fly. Code:
If oNodePassed.Text = "true" Then aCC.XMLMapping.SetMapping XPath:="/ns0:CC_Map_Root[1]/ns0:ContentNode[1]", Source:=oCXMLPart Else aCC.XMLMapping.SetMapping XPath:="/ns0:CC_Map_Root[1]/ns0:NullNode[1]", Source:=oCXMLPart End If
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Andrew,
Of course! I didn't see the tree for the forest. If memory wasn't fading, I could probably say that I started with something like that years ago and wallowed around in frustration until I came up with the Rube Goldberg method posted earlier. In the update attached, I've revised the code more along your suggestion. Instead of the Document variable, I am using a fourth node to serve as the show\hide validator. The method seems to work relatively well. Hopefully one day MS will fix these event issues. Thanks for posting and for the prod ;-) Last edited by gmaxey; 05-03-2024 at 12:57 PM. |
#4
|
|||
|
|||
I have managed to refine this process further and now have a work around that is completely transparent to the user. The setup is a little tricking so I decided to just post a macro that does everything for a potential user.
1. Copy all of the following code to the ThisDocument module of the document vbProject: Code:
Option Explicit Dim WithEvents oCXMLPart As CustomXMLPart Dim m_oNodeValidated As CustomXMLNode Sub SetUpAShowHideProcessInMyWordDocument() Dim oCC As ContentControl Dim oRng As Range Dim oCXMLPart As CustomXMLPart 'This macro: '1) Inserts a CustomXMLPart in your document. Set oCXMLPart = ActiveDocument.CustomXMLParts.Add("<?xml version=""1.0""?><CC_Map_Root xmlns=""http://TheAnchorage/DataXMLPart""><Content></Content><ShowHide></ShowHide><Null></Null><Validated></Validated></CC_Map_Root>") DoEvents '2) Inserts a rich text content control at the selection insertion point. Set oRng = Selection.Range Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, oRng) 'or 'Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, oRng) With oCC .Tag = "DocCC" .SetPlaceholderText , , ChrW(8203) If oCC.Type = wdContentControlText Then .MultiLine = True '3) Maps the CC to the CustomXMLPart Content node. .XMLMapping.SetMapping "/ns0:CC_Map_Root[1]/ns0:Content[1]", , oCXMLPart .Range.Text = "Enter your text or other content here" End With Set oRng = oCC.Range With oRng .Collapse wdCollapseEnd .Move wdCharacter .InsertAfter vbCr .Collapse wdCollapseEnd .Paragraphs(1).SpaceBefore = 12 End With oRng.Select '4) Inserts a checkbox CC in your document. Set oCC = ActiveDocument.ContentControls.Add(wdContentControlCheckBox, oRng) With oCC .Tag = "ShowHide" '5) Maps the CC to the CustomXMLPart ShowHide node. .XMLMapping.SetMapping "/ns0:CC_Map_Root[1]/ns0:ShowHide[1]", , oCXMLPart .Checked = True End With InitializeCPEventMonitor lbl_Exit: Exit Sub End Sub Sub AutoOpen() InitializeCPEventMonitor lbl_Exit: Exit Sub End Sub Sub InitializeCPEventMonitor() Dim oCC As ContentControl Set oCC = ActiveDocument.SelectContentControlsByTag("ShowHide").Item(1) Set oCXMLPart = oCC.XMLMapping.CustomXMLPart Set m_oNodeValidated = oCXMLPart.SelectSingleNode("/ns0:CC_Map_Root[1]/ns0:Validated[1]") m_oNodeValidated.Text = "true" lbl_Exit: Exit Sub End Sub Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl) Select Case ContentControl.Tag Case "DocCC" If oCXMLPart Is Nothing Then InitializeCPEventMonitor 'Update validator node. This suppresses (bypasses) the "ShowHide" events until afer the DocCC content update is performed. m_oNodeValidated.Text = "false" End Select lbl_Exit: Exit Sub End Sub Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Tag Case "DocCC" 'Update the validator node. This enables the "ShowHide" events. m_oNodeValidated.Text = "true" DoEvents 'Evaluate the ShowHide node state. This is necessary due to the event and sequence errors that Microsoft has chose not to resolve. EvalShowHideNode End Select End Sub Private Sub oCXMLPart_NodeAfterReplace(ByVal OldNode As Office.CustomXMLNode, ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when a CP text node is replaced with a new text node value. ProcessChange NewNode lbl_Exit: Exit Sub End Sub Sub ProcessChange(oNodePassed As Office.CustomXMLNode) Select Case oNodePassed.ParentNode.BaseName Case "ShowHide" If m_oNodeValidated.Text = "true" Then 'The ContentNode has updated. The validator is true. Permit show/hide. If oNodePassed.Text = "true" Then 'Map the document content control to the content node. ActiveDocument.SelectContentControlsByTag("DocCC").Item(1).XMLMapping.SetMapping XPath:="/ns0:CC_Map_Root[1]/ns0:Content[1]", Source:=oCXMLPart Else 'Map the document content control to the content node. ActiveDocument.SelectContentControlsByTag("DocCC").Item(1).XMLMapping.SetMapping XPath:="/ns0:CC_Map_Root[1]/ns0:Null[1]", Source:=oCXMLPart End If Else 'The real time ShowHide node event fired first. 'The Validated node value is false. The content in the DocCC has not yet updated to the ContentNode. 'This will happen if the user clicked the ShowHide checkbox directly from the DocCC (without first physically exiting the DocCC). 'Set the validated node to true m_oNodeValidated.Text = "true" 'The Content node will update next. 'When it does, we will evaluate the ShowHide node again and the conditions will be met to enable the ShowHide events. End If Case "Content" 'Evaluate the ShowHide node state. This is necessary due to the event and sequence errors that Microsoft has chose not to resolve. EvalShowHideNode End Select lbl_Exit: Exit Sub End Sub Sub EvalShowHideNode() Dim oNode As CustomXMLNode 'This text node contains the "true" or "false" value of the CP ShowHide node. Set oNode = oCXMLPart.SelectSingleNode("/ns0:CC_Map_Root[1]/ns0:ShowHide[1]/text()[1]") ProcessChange oNode End Sub 2. Put the cursor in the document where you want your content to appear. 3. Run the procedure "SetUpAShowHideProcessInMyWordDocument" 4. Viola |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Show Hide text using Radio buttons | Costtx | Word | 2 | 10-26-2021 04:39 AM |
Show/Hide text based on dropdown value | AOSMITH | Word VBA | 2 | 11-19-2019 02:19 PM |
How to use checkbox to show/hide bookmarked text? | namrehx | Word VBA | 16 | 12-14-2017 01:45 PM |
How to use checkbox to show and hide bookmarked text? | namrehx | Word VBA | 1 | 12-12-2017 02:17 PM |
Hide/Show text based upon check box status | nathan.gray@emerson.com | Word VBA | 5 | 12-08-2017 01:08 PM |