Macro to display text in Word form

B

Barry

I need to create a Word macro/vba script to display text to the right of a
form field if a particular field value is select from the list of values.
Can someone give me an example vba script?
 
B

Barry

I currently have a script event titled

Private Sub Dropdown8_Click()

which contains currently only:

Code:
Set myTable = ActiveDocument.Tables(1)
If myTable.Dropdown8 = "Ogallala" Then
myTable.Text22 = "SAR"

but does not yet do anything.

Also tried the below, to no avail:

Code:
strText = Selection.Text
strText2 = Text22
If strText = "Ogallala" Then
Set strText2.Text = "SAR"
 
B

Barry

I adjusted the script that works (below), but only changes the text if
manually running the script after changing the value in Dropdown8. Can
someone help me to figure out how to automatically run the script (or can be
macro) when the document opens and continually runs so that it(Text22) is
updated each time the value in Dropdown8 is changed?

Code:
If ActiveDocument.FormFields("Dropdown8").Result = "Ogallala" Then
ActiveDocument.FormFields("Text22").Result = "SAR Model Needed"
Else
ActiveDocument.FormFields("Text22").Result = " "
End If
 
B

Barry

I figured out that it does change the text in Text22, based on selection in
Dropdown8, only if you select, tab out, and select it a second time. Any
ideas how to update it to change each time a selection is made (in Dropdown8)?
 
B

Barry

I added

Code:
Application.ScreenUpdating = False
to the beginning of script
event, and

Code:
Application.ScreenUpdating = True
to the end of the script
event, which works as long as tab out before selecting another value. But,
if try to change value (of Dropdown8), without tabbing out, (Text22) does not
update.
 
G

Graham Mayor

You don't need a macro to do that (and it is probably better not to use
macros in a form if you can avoid it), you can do it with a conditional
field. Using the fieldname you have quoted elsewhere in the thread
you should check the calculate on exit check box property of the field then
insert a conditional field thus

{IF {REF Dropdown8} = "Ogallala" "SAR" ""}


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

Barry

Yes, that works fine, thanks very much!

Graham Mayor said:
You don't need a macro to do that (and it is probably better not to use
macros in a form if you can avoid it), you can do it with a conditional
field. Using the fieldname you have quoted elsewhere in the thread
you should check the calculate on exit check box property of the field then
insert a conditional field thus

{IF {REF Dropdown8} = "Ogallala" "SAR" ""}


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top