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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2010 Form Field Default Text not updated by macro 1

Status
Not open for further replies.
Jan 12, 2011
18
GB
Hello People,

I have written a macro to correct some basic syntax errors in form fields used in a large number of documents. Another macro ensures that this one is run on each document in a given folder. This macro will check through the document for specific known errors in the default text of the form fields and will replace these with known corrections. Here is the macro:

Sub CorrectDefaultText(WdDoc As Document)
Dim oFrmFlds As FormFields
Dim pIndex As Long
Dim iFFs As Integer
Dim strName As String
'all the Form Fields in the document
Set oFrmFlds = ActiveDocument.FormFields
'the number of Form Fields in the document
iFFs = oFrmFlds.Count
'run through each form field
For pIndex = 1 To iFFs
oFrmFlds(pIndex).Select
'test the default text for syntax / spelling errors and inconsistencies
Select Case oFrmFlds(pIndex).TextInput.Default
Case "Adressee Adr1"
oFrmFlds(pIndex).TextInput.Default = "Addressee Adr1"

Case "Adressee Adr2"
oFrmFlds(pIndex).TextInput.Default = "Addressee Adr2"

Case "Adressee Adr3"
oFrmFlds(pIndex).TextInput.Default = "Addressee Adr3"

Case "Adressee Adr4"
oFrmFlds(pIndex).TextInput.Default = "Addressee Adr4"

Case "Adressee Postcode"
oFrmFlds(pIndex).TextInput.Default = "Addressee Postcode"

Case "Commercial Name"
oFrmFlds(pIndex).TextInput.Default = "Product Name"

Case "Product"
oFrmFlds(pIndex).TextInput.Default = "Product Name"

Case "ref"
oFrmFlds(pIndex).TextInput.Default = "Ref"

Case "Branded Policy email"
oFrmFlds(pIndex).TextInput.Default = "Branded Policy Email"

Case "Destination"
oFrmFlds(pIndex).TextInput.Default = "Claim Country"

Case "Branded Policy Name"
oFrmFlds(pIndex).TextInput.Default = "Branded Policy Adr1"
End Select
Next pIndex
End Sub

Well that's all fine, it works and no bugs etc. However, you can't see that it has worked until you double click on each form field and then click "OK" So the default text is not displayed until then. How can I force Word to update the displayed text to match the newly changed default text?

Thanks for your time and I am of course willing to answer more questions if anything does not seem to make sense to you :)
 
Hi HighPlainsGrifter,

You need to test whether the default is displayed. Simply changing the default doesn't change what is displayed (you wouldn't want it to if something other than the default was displayed) and, after changing the default, whatever was displayed is most likely not the same as the default anymore.

Try it this way:
Code:
Sub CorrectDefaultText(WdDoc As Document)
Dim oFrmFld As FormField, bUpdate As Boolean
'run through each form field
For Each oFrmFld In WdDoc.FormFields
  bUpdate = False
  With oFrmFld.TextInput
    If .Default = oFrmFld.Result Then bUpdate = True
    'test the default text for syntax / spelling errors and inconsistencies
    Select Case .Default
      Case "Adressee Adr1"
        .Default = "Addressee Adr1"
      Case "Adressee Adr2"
        .Default = "Addressee Adr2"
      Case "Adressee Adr3"
        .Default = "Addressee Adr3"
      Case "Adressee Adr4"
        .Default = "Addressee Adr4"
      Case "Adressee Postcode"
        .Default = "Addressee Postcode"
      Case "Commercial Name"
        .Default = "Product Name"
      Case "Product"
        .Default = "Product Name"
      Case "ref"
        .Default = "Ref"
      Case "Branded Policy email"
        .Default = "Branded Policy Email"
      Case "Destination"
        .Default = "Claim Country"
      Case "Branded Policy Name"
        .Default = "Branded Policy Adr1"
    End Select
    If bUpdate = True Then oFrmFld.Result = .Default
  End With
Next oFrmFld
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top