Forum Discussion
Automatically update text depending on contents of a content control in Word
Hi, is it possible to automatically update part of a Word document using content controls and perhaps field codes or something similar? I'll outline an example here:
I have a content control that will have "Phase 1", "Phase 2" or "Phase 3" written in it. Depending what is written I want a piece of text elsewhere in the document to automatically update with some corresponding text. For example if the content control reads "Phase 1" then the text will update to say "One", similarly "Phase 2" will update the text with "Two" etc. Is this possible? I have found various bit of code online but they return a blank outcome or incorrect text. Many thanks for any response.
ChrisUoN Firstly, to control what appears in the Content Control, I would recommend that you use a Combo Box Content Control that contains the Phase 1, Phase 2 and Phase 3 as the only items that can be selected. Then set the Title of the Content Control to "Phase", then paste the following code into the ThisDocument Object in the Visual Basic Editor:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) If ContentControl.Title = "Phase" Then Select Case ContentControl.Range.Text Case "Phase 1" ThisDocument.Variables("varPhase").Value = "One" Case "Phase 2" ThisDocument.Variables("varPhase").Value = "Two" Case "Phase 3" ThisDocument.Variables("varPhase").Value = "Three" Case Else ThisDocument.Variables("varPhase").Value = "Nothing Selected" End Select End If ThisDocument.Range.Fields.Update End Sub
- ChrisUoNCopper Contributor
Doug_Robbins_Word_MVP
Thanks a lot, Doug! That works great.Is there any way something like this can work in Word online? I assume Visual Basic can't be used online.
ChrisUoN Hi Chris,
Compared to the Desktop application, the On-Line Word application is not good for anything much other than reading documents
- DpaulheskCopper ContributorHow does one show the results of this in the Word document? I can't tell from here how to show the output. Thanks.
- Charles_KenyonBronze Contributor
The results of a choice in a ComboBox or Drop-Down Content Control in Word are shown in the control in the document.
To have the results show up elsewhere you need a mapped control or another method.