Results 1 to 1 of 1
  1. #1
    silverspr is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    19

    Automate word from Access to output to txt file for import

    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
    Last edited by silverspr; 03-07-2012 at 09:03 PM. Reason: It was working...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Automate import into multiple Access tables
    By mcchung52 in forum Import/Export Data
    Replies: 5
    Last Post: 01-26-2012, 05:33 PM
  2. Replies: 0
    Last Post: 08-20-2010, 09:26 AM
  3. Question on Macro to automate file import
    By delkath in forum Access
    Replies: 4
    Last Post: 05-25-2010, 04:28 AM
  4. Automate Excel Import to Access Related Tables
    By KramerJ in forum Programming
    Replies: 6
    Last Post: 04-04-2009, 04:24 PM
  5. Replies: 1
    Last Post: 09-06-2006, 11:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums