There are a number of threads dealing with Word FormField issues. Here are a couple of routines that may be useful. They may not cover all your needs (not likely!), but they will hopefully get you started, and they certainly can be modified, and/or combined, to cover a lot of issues.
Code #1
What it could be used for: Counting Formfields, Making an array of a certain Formfield type (Textbox in this example) and the values for each of that type.
Code #2
What it could be used for: Clears all formfields. Use this as the the exit macro on the last field, or as a separate closing macro to the document. It checks for default text, and if the textbox has default text, it resets the result to taht dfault again.
Code #3
What this could be used for: This is an example of looping through a number of files and retrieving data from them. This examples gets a piece of information from each document in a folder and calculates an average from the total collected. It could also be use for picking up strings for formfields. maybe use this in conjunction with other code above to create an array of text from various files.
Hope this helps.
Gerry
Code #1
What it could be used for: Counting Formfields, Making an array of a certain Formfield type (Textbox in this example) and the values for each of that type.
Code:
Sub TextFFArray()
Dim mFormField As FormField
Dim i As Integer
Dim intFFCount As Integer
Dim intTextFFCount As Integer
Dim TextFormFields() As String
Dim sMessage As String
Dim var
For Each mFormField In ActiveDocument.FormFields()
[COLOR=red]' increase total count of fields[/color red]
intFFCount = intFFCount + 1
If mFormField.Type = wdFieldFormTextInput Then
[COLOR=red]' increase total count of text fields[/color red]
intTextFFCount = intTextFFCount + 1
ReDim Preserve TextFormFields(i)
TextFormFields(i) = mFormField.Name
i = i + 1
End If
Next
ReDim Preserve TextFormFields(i)
[COLOR=red] ' may as well use this again[/color red]
i = 0
[COLOR=red] ' loop through array getting field name &
' using it to get resulting value[/color red]
For var = 1 To intTextFFCount
sMessage = sMessage & _
"Field name:= " & TextFormFields(i) & " " & _
"Value:= " & ActiveDocument.FormFields(TextFormFields(i)).Result & _
vbCrLf
i = i + 1
Next
[COLOR=red]' display message with:
' total number of fields,
' total number of textboxes
' each textbox name and result[/color red]
MsgBox "There are " & intFFCount & " formfields in this " & _
"document, including " & intTextFFCount & " textboxes." & _
vbCrLf & " The contents of those textboxes are:" & vbCrLf & _
sMessage
End Sub
Code #2
What it could be used for: Clears all formfields. Use this as the the exit macro on the last field, or as a separate closing macro to the document. It checks for default text, and if the textbox has default text, it resets the result to taht dfault again.
Code:
Sub ClearAllFormFields()
Dim myFormField As FormField
Dim aDoc As Document
Dim response
Dim msg As String
Set aDoc = ActiveDocument
msg = "Do you want to clear all formfield results?"
response = MsgBox(msg, vbYesNo)
If response = vbYes Then
For Each myFormField In aDoc.FormFields()
Select Case myFormField.Type
Case 70 [COLOR=red}' text[/color red}
[COLOR=red] ' checks to see if there is default text
' if yes, removes user inout and
' reverts to default; if no, removes user input[/color red]
If myFormField.TextInput.Default <> "" Then
myFormField.result = myFormField.TextInput.Default
Else
myFormField.result = ""
End If
Case 71 [COLOR=red]' check[/color red]
myFormField.result = False
Case 83 [COLOR=red]' dropdown[/color red]
myFormField.DropDown.Value = 1
End Select
Next
Else
End If
Set aDoc = Nothing
End Sub
Code #3
What this could be used for: This is an example of looping through a number of files and retrieving data from them. This examples gets a piece of information from each document in a folder and calculates an average from the total collected. It could also be use for picking up strings for formfields. maybe use this in conjunction with other code above to create an array of text from various files.
Code:
Sub GetAv()
[COLOR=red]' assumes each file has a formfield named GetAv
' and that formfield has a piece of text that is a number
' Probably should add a error trap (IsNumeric) to ensure
' that content of text formfield is, in fact numeric![/color red]
Dim aDoc
Dim ThisDoc As Document
Dim i As Integer
Dim lngTotalCount As Long
Dim lngCurNumber As Long
Dim strMessage As String
[COLOR=red] ' this does not have to be hard coded[/color red]
aDoc = Dir("c:\TempRun\*.DOC")
On Error Resume Next
Do While aDoc <> ""
[COLOR=red]' open the file and make it document object[/color red]
Application.Documents.Open FileName:=aDoc
Set ThisDoc = ActiveDocument
[COLOR=red]' get the formfield text and convert to number [/color red]
lngCurNumber = CLng(ThisDoc.FormFields("getAv").Result)
lngTotalCount = lngTotalCount + lngCurNumber
i = i + 1
[COLOR=red]' add filename and value to message string[/color red]
strMessage = strMessage & _
ThisDoc.Name & " " & lngCurNumber & vbCrLf
End If
[COLOR=red]' close current doc, destroy doc object[/color red]
ThisDoc.Close
Set ThisDoc = Nothing
aDoc = Dir()
Loop
[COLOR=red]' display results. This could be changed to putting the average
‘ (lngTotalCount / i ) into somewhere else.[/color red]
MsgBox strMessage & vbCrLf & vbCrLf & _
"Average is: " & lngTotalCount / i
End Sub
Hope this helps.
Gerry