Update Content of TextFormField based on Dropdown Selection

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Update Content of TextFormField based on Dropdown Selection

Post by RaudelJr »

Hi,

I'm trying to figure out a way to add (From the legacy tools) to a word document both a Dropdown & and a Text Form Field.

The Dropdown tool will have different selections, and based on the selection, the Text Form Field will display a different text content.

Is there a way to code this in VB?

Greatly appreciate it in advance.

Raudel

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Update Content of TextFormField based on Dropdown Select

Post by HansV »

Create a macro like this, using the appropriate names and values of course:

Code: Select all

Sub UpdateText()
    Select Case ActiveDocument.FormFields("Dropdown1").Result
        Case "a"
            ActiveDocument.FormFields("Text1").Result = "this"
        Case "b"
            ActiveDocument.FormFields("Text1").Result = "that"
        Case "c"
            ActiveDocument.FormFields("Text1").Result = "other"
    End Select
End Sub
While the document is unprotected, double-click the dropdown.
Select the macro you created to run on Exit:
S2065.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Update Content of TextFormField based on Dropdown Select

Post by HansV »

Create a macro like this, using the appropriate names and values of course:

Code: Select all

Sub UpdateText()
    Select Case ActiveDocument.FormFields("Dropdown1").Result
        Case "a"
            ActiveDocument.FormFields("Text1").Result = "this"
        Case "b"
            ActiveDocument.FormFields("Text1").Result = "that"
        Case "c"
            ActiveDocument.FormFields("Text1").Result = "other"
    End Select
End Sub
While the document is unprotected, double-click the dropdown.
Select the macro you created to run on Exit:
S2065.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Update Content of TextFormField based on Dropdown Select

Post by macropod »

Unless you plan to edit the text formfield, you don't need one - simply use a series of IF fields that reference the dropdown formfield's internal bookmark and check the formfield's 'calculate on exit' property. For example:
{IF{REF Dropdown1}= "Item 1" "Item 1 output text"}{IF{REF Dropdown1}= "Item 2" "Item 2 output text"}{IF{REF Dropdown1}= "Item 3" "Item 3 output text"}
where 'Dropdown1' is the dropdown formfield's internal bookmark name.

Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practical to add them via any of the standard Word dialogues. The spaces represented in the field constructions are all required.
Paul Edstein
[Fmr MS MVP - Word]

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Update Content of TextFormField based on Dropdown Select

Post by RaudelJr »

Thank you, HansV and Macropod, both for your replies,

In the template I'm creating I do need the user to be able to modify the TextFormField if desired even after the drop down has been selected. But on a typical day the standard would be to leave the fields as updated by the macro ran on exit.

The code worked like a charm.

Thank you very much for always being so very helpful.

RaudelJr