![]() |
#4
|
|||
|
|||
![]()
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 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 |
Tags |
vba in microsoft word, vba word |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Sammie0Sue | Word VBA | 21 | 01-12-2021 04:44 PM |
![]() |
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 |
![]() |
Sammie0Sue | Word | 6 | 11-06-2013 10:56 PM |
![]() |
cksm4 | Word VBA | 2 | 03-01-2011 12:46 PM |