Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-17-2014, 07:12 AM
lucky16 lucky16 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 2007
Novice
How to use onchange event for content controls in word
 
Join Date: Jan 2014
Posts: 21
lucky16 is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 05-17-2014, 07:27 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,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 05-17-2014, 03:48 PM
macropod's Avatar
macropod macropod 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 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
where the phone & fax #s are output to a content control named Phone/Fax and the email address is output to a content control named Email.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #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,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
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
  #5  
Old 06-05-2014, 07:35 AM
lucky16 lucky16 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 2007
Novice
How to use onchange event for content controls in word
 
Join Date: Jan 2014
Posts: 21
lucky16 is on a distinguished road
Default

THANK YOU very much Maxey.
Reply With Quote
  #6  
Old 06-05-2014, 11:04 PM
NobodysPerfect NobodysPerfect is offline How to use onchange event for content controls in word Windows 8 How to use onchange event for content controls in word Office 2010 32bit
Competent Performer
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 06-06-2014, 04:52 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,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 06-06-2014, 05:05 AM
NobodysPerfect NobodysPerfect is offline How to use onchange event for content controls in word Windows 8 How to use onchange event for content controls in word Office 2010 32bit
Competent Performer
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

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
Reply With Quote
Reply

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 02:01 PM.


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