Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - macro to display text on form

Status
Not open for further replies.

BGuidry

Technical User
Mar 18, 2009
28
US
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?
 
I currently have a script event titled

Private Sub Dropdown8_Click()

which contains currently only:

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"
 
I adjusted to this 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
 
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.
 
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.
 
1. Using default names is not a good idea. "Dropdown8", "Text22" etc.

2. You are using - apparently = a combination of both formfields AND ActiveX controls. Dropdown8_Click() is an ActiveX control event, formfields do NOT have a Click event...yet you also have: ActiveDocument.FormFields("Dropdown8").Result

So which is it? A formfield, or an ActiveX control?

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

but does not yet do anything.

Yes, I am not surprised it did not. Are you using Option Explicit? myTable.Dropdown8 should fail, as this is incorrect syntax for the object.

4. "when the document opens and continually runs "

My bolding. No, there is nothing that continually runs. Good thing too. There are OnExit macros for foemfields. Are you using them...who knows, you do not say. It seems you may be, but I am not totally sure. It would help if you stated exactly what you are doing.

"when the document opens" and "continually runs " are mutually exclusive. Having it update on doc open is easy. Use the Document_Open event. Or update it when the document is closed...whichever. But that is a different issue from updating when the dropdown is changed. The OnExit macro will work, but yes, you have to tab out for it to fire. OnExit.

If the dropdown was an ActiveX control, then you could use a _Change event.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top