Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 05-18-2014, 07:42 AM
gmaxey gmaxey is offline How to use onchange event for content controls in word Windows 7 32bit How to use onchange event for content controls in word 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

Paul,

Thanks for clearing that up.

Lucky,

Again there is no built-in or easy "OnChange" event for content controls. While Paul's method illustrated above will likely work, it is triggered "OnExit" and you can't be sure that the document user will exit.

If using a dropdown box to make a selection, you can use mapped content controls and create a pseudo onchange event as illustrated in the following example:

1. Add a dropdown CC and three plain text CCs to a document. Title the dropdown "Name" and title the plain text CCs "Phone" "Fax" and "E-Mail" set each of the plain text properties "content cannot be edited."

2. Map all four CCs to a customXMLPart by running the following code:

Code:
Sub MapTheCCs()
Dim oXMLPart As CustomXMLPart
Dim lngIndex
  With ActiveDocument
    Set oXMLPart = .CustomXMLParts.Add("<?xml version='1.0'?>" _
                                   & "<CC_Map xmlns='http://Greg_Maxey/CC_Mapping_Part'>" _
                                   & "<Name></Name>" _
                                   & "<Phone></Phone>" _
                                   & "<Fax></Fax>" _
                                   & "<E-Mail></E-Mail>" _
                                   & "</CC_Map>")
    With .SelectContentControlsByTitle("Name").Item(1)
      With .DropdownListEntries
        For lngIndex = .Count To 2 Step -1
          .Item(lngIndex).Delete
        Next
        .Add "Al", "Al"
        .Add "Bob", "Bob"
        .Add "Charlie", "Charlie"
      End With
 
      .XMLMapping.SetMapping ("/ns0:CC_Map[1]/ns0:Name[1]")
    End With
    .SelectContentControlsByTitle("Phone").Item(1).XMLMapping.SetMapping ("/ns0:CC_Map[1]/ns0:Phone[1]")
    .SelectContentControlsByTitle("Fax").Item(1).XMLMapping.SetMapping ("/ns0:CC_Map[1]/ns0:Fax[1]")
    .SelectContentControlsByTitle("E-Mail").Item(1).XMLMapping.SetMapping ("/ns0:CC_Map[1]/ns0:E-Mail[1]")
  End With
lbl_Exit:
  Exit Sub
End Sub
3. Use the built-in content control before store update event as a pseudo change event using the following code in the ThisDocument module:

Code:
Private Sub Document_ContentControlBeforeContentUpdate(ByVal ContentControl As ContentControl, _
                     Content As String)
Dim oXMLPart As CustomXMLPart
Dim oNode As CustomXMLNode
  Select Case ContentControl.Title
    Case "Name"
      Set oXMLPart = ActiveDocument.CustomXMLParts. _
          SelectByNamespace("http://Greg_Maxey/CC_Mapping_Part").Item(1)
      With oXMLPart
        Set oNode = .SelectSingleNode("/ns0:CC_Map[1]/ns0:Name[1]")
        Select Case oNode.Text
          Case "Al"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:Phone[1]").Text = "123-456-7890"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:Fax[1]").Text = "123-456-7891"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:E-Mail[1]").Text = "al@gmail.com"
          Case "Bob"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:Phone[1]").Text = "686-333-1121"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:Fax[1]").Text = "686-433-1122"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:E-Mail[1]").Text = "bob@yahoo.com"
          Case "Charlie"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:Phone[1]").Text = "978-221-3212"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:Fax[1]").Text = "978-221-3213"
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:E-Mail[1]").Text = "charlie@attnet.com"
          Case Else
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:Phone[1]").Text = vbNullString
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:Fax[1]").Text = vbNullString
            .SelectSingleNode("/ns0:CC_Map[1]/ns0:E-Mail[1]").Text = vbNullString
        End Select
      End With
  End Select
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 

Tags
vba in microsoft word, vba word



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use onchange event for content controls in word VBA to set Content controls as non printing Sammie0Sue Word VBA 21 01-12-2021 04:44 PM
How to use onchange event for content controls in word MS Word 2010 Content Controls and PDF Conversion. MaxInCO Word 3 02-13-2014 12:58 PM
Word 2010 Macro Enabled Template with Content Controls keen1598 Word VBA 7 01-29-2014 03:17 PM
How to use onchange event for content controls in word Content Controls Sammie0Sue Word 6 11-06-2013 10:56 PM
How to use onchange event for content controls in word Grouping Content Controls cksm4 Word VBA 2 03-01-2011 12:46 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:47 PM.


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