#1
|
|||
|
|||
How to use onchange event for content controls in word
Hi,
What code should be used for a situation like changing a value from a content control, changes the value of another content control. I know that this can be performed with the use of "|" from content control properties. But in my case there are more than two content controls and also there lies some text between each. Can you please help with some sample code on how to do this. Thank you! Happy weekend! |
#2
|
|||
|
|||
What is use of "|" from content control properties?
Unfortunately in no version of Word is there a built in onchange event. What do want to happen to the other CCs when you change one? If simply replicate the content then you need to map them to a common XML data node. If you want variable change, then you will need to create a custom onchange event. It is no walk in the sun, but it can be done: http://gregmaxey.com/word_tip_pages/...om_events.html |
#3
|
||||
|
||||
I suspect it's a reference to a technique I used here: https://www.msofficeforums.com/word-...own-lists.html, where the | has nothing to do with content control properties but with parsing some data stored in the content control value. Using the approach in that document, one might also use:
Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Dim i As Long, StrDetails As String With ContentControl If .Title = "Client" Then For i = 1 To .DropdownListEntries.Count If .DropdownListEntries(i).Text = .Range.Text Then StrDetails = .DropdownListEntries(i).Value Exit For End If Next With ActiveDocument With .SelectContentControlsByTitle("Phone/Fax")(1).Range If StrDetails <> "" Then .Text = Split(StrDetails, "|")(0) & Chr(11) & Split(StrDetails, "|")(1) Else .Text = "" End If End With With .SelectContentControlsByTitle("Email")(1).Range If UBound(Split(StrDetails, "|")) > 0 Then .Text = Split(StrDetails, "|")(2) Else .Text = "" End If End With End With End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#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 |
#5
|
|||
|
|||
THANK YOU very much Maxey.
|
#6
|
|||
|
|||
Hi Greg,
great solution, answers many questions. But I have an additional question. I (almost) followed your instructions and inserted the four Content Controls, but mistakenly I inserted rich text (instead of plain text) Content Controls. Running the MapCCs code I got runtime error '6191' ("Data connection cannot be created for Rich Text Content Control" - translated). WHY??? I know mapping Rich Text Content Controls IS possible, so which are the lines in the code to be adjusted to map RT CCs? Thanks NP Last edited by NobodysPerfect; 06-06-2014 at 04:47 AM. |
#7
|
|||
|
|||
No problem here if using Word 2013. Are you sure you are using Word 2013?? You can't map rich text type controls in earlier versions.
|
#8
|
|||
|
|||
No, I'm sure, I'm not (using 2013). So I only though I could ...
Strange it seems I've always inserted plain text CCs by chance. Must have been so ... learning never stops. Thx for the answer NP |
Tags |
vba in microsoft word, vba word |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA to set Content controls as non printing | Sammie0Sue | Word VBA | 21 | 01-12-2021 04:44 PM |
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 |
Content Controls | Sammie0Sue | Word | 6 | 11-06-2013 10:56 PM |
Grouping Content Controls | cksm4 | Word VBA | 2 | 03-01-2011 12:46 PM |