Hello
I have a number of word doc forms with 65 form fields, I am trying to automate the process of importing these word docs from a directory after outputting to a text file, then import into Access. I am currently looping through the directory of word docs, opening each one and then trying to output each field to ideally one text file or if I must I'll save a txt file for each word doc. The output fails silently, but when I run the routine (OutputFld) from within word it works. I've attached my code, GetWData, opens the word docs, OutputFld loops through the document and outputs the fields to a text file. Some directories have 65 word docs, I would prefer to output to one txt file....coudn't get this to work. thanks for your help.
Function GetWData()
Dim appWord As Word.Application
Dim doc As Word.Document
Dim MyDB As Database
Dim rst As Recordset
Dim strDocName As String
Dim blnQuitWord As Boolean
On Error GoTo ErrorHandling
strdir = "g:\2012pa\chrisl\"
strfile = Dir("g:\2012pa\chrisl\")
Do While strfile <> ""
strDocName = strdir + strfile
Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)
'With doc
Call CntFld
'End With
doc.Close
strfile = Dir()
Loop
If blnQuitWord Then appWord.Quit
MsgBox "complete"
Cleanup:
Set rst = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Function
ErrorHandling:
Select Case Err
Case -2147022986, 429
Set appWord = CreateObject("Word.Application")
blnQuitWord = True
Resume Next
Case 5121, 5174
MsgBox "You must select a valid Word document. " _
& "No data imported.", vbOKOnly, _
"Document Not Found"
Case 5941
MsgBox "The document you selected does not " _
& "contain the required form fields. " _
& "No data imported.", vbOKOnly, _
"Fields Not Found"
Case Else
MsgBox Err & ": " & Err.Description
End Select
GoTo Cleanup
End Function
'*******************
'Function to output form fields
Sub OutputFld()
Dim count As Integer
'Dim afield As Word.Field
On Error Resume Next
'Kill "G:\Desktop\Temp1.txt"
Open "G:\Desktop\Temp1.txt" For Output As #1
count = 0
Print #1, "QID", "Response"
For Each afield In ActiveDocument.FormFields
'If afield.Type = wdFieldFormTextInput Then
count = count + 1
'Debug.Print count & "," & afield.Result
Print #1, """" & count & """" & Chr(59) & """" & afield.Result & """"
'End If
Next afield
'Close #1
MsgBox "output complete"
End Sub