View Single Post
 
Old 05-04-2024, 09:00 AM
gmaxey gmaxey is offline Windows 10 Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote