Search code examples
vbams-officems-word

Converting multiple MS Word documents into txt files (few extra bits)


I'm currently working on transferring a lot of MS Word forms into a database system.

My approach is currently:

  1. Open individual word docs
  2. Go to advanced options to change save settings so that only form data is saved as delimited text file
  3. Save and click OK for pop up prompt windows
  4. Use cmd to merge all txt files
  5. Import into excel and process there

Hopefully at the end of these stages, I will obtain a fairly collated excel file of information that can be transferred onto the database system.

My question is, since I have a lot of word documents (and I feel like a robot after going through about 100 of them), can I automate the process of 1) 2) and 3)?

Any help would be much appreciated, I have scripted in python before and done some simple programming, but any solution is welcome.


Solution

  • You can use a VBA macro to automate steps 1-3. Using the Document.SaveAs method you can save the form data only to a txt file:

    ActiveDocument.SaveAs ActiveDocument.FullName & ".txt", _
        WdSaveFormat.wdFormatText, SaveFormsData:=True
    

    Then you can call this SaveAs method in loop over all documents in a given folder:

    Sub SaveAllFormData(path As String)
        Dim doc As Document
        Dim fileName As String
    
        fileName = Dir(path & "*.doc")
    
         ' Loop through all .doc files in that path
        Do While fileName <> ""
            Set doc = Application.Documents.Open(path & fileName)
    
             ' Save form data
            doc.SaveAs2 doc.FullName & ".txt", WdSaveFormat.wdFormatText, SaveFormsData:=True
    
            doc.Close wdDoNotSaveChanges
            fileName = Dir
        Loop
    End Sub
    

    If you need help setting up and running the macro, check out the docs: Create or run a macro