View Single Post
 
Old 02-17-2014, 09:16 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
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

If I select the first choice .. Test2 should automatically choose the same.

As you've probably seen, there is no built-in change event for content controls so "automatically" is really not possible. You have to select the first choice then "exit."

There are several ways to work around this. But, why does the 2nd CC need to be a dropdown? If you select the second choice in test 1, all you really want is for Test2 to display "COOT" Correct?

One method is to map the first CC to a CustomXMLPart and then use one of the built-in CustomXMLPart events:

Here is how:

In a new blank document. Add a standard code module and paste in this code:

Code:
Option Explicit
Sub AddCCsAndMap()
Dim oCC As ContentControl
Dim oRng As Word.Range
With ActiveDocument
  .Range.Text = "Select English Term: "
  Set oRng = .Range
  oRng.Collapse wdCollapseEnd
  Set oCC = .ContentControls.Add(Type:=wdContentControlDropdownList, Range:=oRng) '.Range) 'Selection.Range)
  With oCC
    .Title = "Master"
    With .DropdownListEntries
      .Add "Chose an item.", "Nothing selected."
      .Add "A"
      .Add "B"
    End With
  End With
  oRng.Start = .Range.End - 1
  oRng.InsertBefore vbCr
  oRng.Start = .Range.End - 1
  oRng.Text = "Russian Equivalent: "
  oRng.Start = .Range.End - 1
  Set oCC = .ContentControls.Add(wdContentControlText, oRng)
  With oCC
    .Title = "Slave"
    .LockContents = True
    .SetPlaceholderText , , "Nothing chosen."
  End With
  .Range.InsertAfter vbCr + vbCr & "There is an apparent ""bug"" or at least a design flaw in the NodeAfterReplace event." & vbCr _
                     & "If the Content Control XML mapping node value is null (i.e., Content control displaying normal Placeholder text) the event is not triggered when" _
                     & " the null value is replaced with a non-null value (i.e., a Content Control item is selected).  Additionally the event is not triggered" _
                     & " when a non-null value is replaced with a null value.  The event is only triggered when a non-null value is replaced with another non-null value." & vbCr & vbCr _
                     & "The work around is to use and store a non-null value in the XML node that mimics placeholder text.  CC.Range" _
                     & " formatting attributes are used to make the imposter text appear as placeholder text in the content control."
End With
MapCCs
AutoOpen
End Sub
Sub MapCCs()
Dim oCC As ContentControl
Dim strXML As String
Dim oCustXMLPart As CustomXMLPart
  strXML = "<?xml version='1.0' encoding='utf-8'?><Root><Master> </Master></Root>"
  ClearXMLParts
  Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add(strXML)
  Set oCC = ActiveDocument.SelectContentControlsByTitle("Master").Item(1)
  oCC.XMLMapping.SetMapping "/Root/Master[1]"
  
  Set oCustXMLPart = Nothing
  AutoOpen
  oCC.DropdownListEntries(2).Select
  oCC.DropdownListEntries(1).Select
  Set oCC = Nothing
End Sub
Sub AutoOpen()
  ThisDocument.InitializeMonitor
End Sub
 Sub ClearXMLParts()
Dim i As Long
For i = ActiveDocument.CustomXMLParts.Count To 4 Step -1
  ActiveDocument.CustomXMLParts(i).Delete
Next i
End Sub
Then in the ThisDocument module of the document, paste the following code:

Code:
Option Explicit
Dim WithEvents oMonitor As CustomXMLPart
Sub InitializeMonitor()
  On Error Resume Next
  Set oMonitor = ThisDocument.CustomXMLParts(4)
End Sub
Private Sub oMonitor_NodeAfterReplace(ByVal OldNode As Office.CustomXMLNode, ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean)
Select Case NewNode.ParentNode.BaseName
  Case "Master"
    ActiveDocument.SelectContentControlsByTitle("Master").Item(1).Range.Font.Color = wdColorAutomatic
    ActiveDocument.SelectContentControlsByTitle("Slave").Item(1).LockContents = False
    Select Case oMonitor.SelectSingleNode("/Root/Master[1]").Text
      Case "Nothing selected."
        ActiveDocument.SelectContentControlsByTitle("Master").Item(1).Range.Font.Color = wdColorGray45
        With ActiveDocument.ContentControls(2).Range
          .Text = "Nothing chosen."
          .Font.Color = wdColorGray45
        End With
      Case "A"
        With ActiveDocument.ContentControls(2).Range
          .Text = "Your ""A"" Russian term here."
          .Font.Color = wdColorGreen
        End With
      Case "B"
        With ActiveDocument.ContentControls(2).Range
          .Text = "Your ""B"" Russian term here."
          .Font.Color = wdColorBlue
        End With
   End Select
   ActiveDocument.SelectContentControlsByTitle("Slave").Item(1).LockContents = True
End Select
End Sub
Now open the standard module and execute the procedure named:
AddCCsAndMap
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote