Excel VBA - Open Word Doc, Save in MS DOS Text Format, Quit Word

JoeDBugger

New Member
Joined
Apr 22, 2009
Messages
9
Using Excel VBA, I want to process all Word documents in the working directory saving them in text format. The processing will include: (1) open a Word document, (2) save that Word document in MS DOS text format, and (3) quit Word. The following VBA Macro is invoked for each Word document:

Code:
Sub ProcessWordDocument(ByVal psWorkbookCurrentWorkingDirectory As String, _
                        ByVal psNextFullChecklistFileName As String, _
                        ByVal psNextChecklistFileName As String)
 
    Dim wdApplication As Word.Application
    Dim wdDocument As Word.Document
 
    On Error Resume Next
    Set wdApplication = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        '----+----+----+----+----+----+----+----+----+----+----+----+
        '  Word is NOT already running
        '----+----+----+----+----+----+----+----+----+----+----+----+
        Set wdApplication = CreateObject("Word.Application")
    End If
    On Error GoTo 0
 
    Set wdDocument = wdApplication.Documents.Open(psWorkbookCurrentWorkingDirectory & "\" & psNextFullChecklistFileName)
 
    wdApplication.Visible = True
 
    wdDocument.SaveAs psWorkbookCurrentWorkingDirectory & "\" & psNextChecklistFileName & ".txt"
 
    wdDocument.Close
 
    wdApplication.Quit
 
    Set wdApplication = Nothing
    Set wdDocument = Nothing
 
End Sub    '  End Sub ProcessWordDocument

The problem with the VBA code is with the statement:

Code:
    wdDocument.SaveAs psWorkbookCurrentWorkingDirectory & "\" & psNextChecklistFileName & ".txt"

The resultant file contains "garbage," of course. I need some mechanism to indicate the "FileFormat" to the ".SaveAs".

Thanks,
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I just answered my own question using the following statement instead of the one that didn't work:

Code:
    wdDocument.SaveAs Filename:=psWorkbookCurrentWorkingDirectory & "\" & _
                      psNextChecklistFileName & ".txt", _
                      FileFormat:=wdFormatText, LockComments:=False, Password:="", _
                      AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
                      EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
                      SaveAsAOCELetter:=False, Encoding:=1252, InsertLineBreaks:=False, _
                      AllowSubstitutions:=False, LineEnding:=wdCRLF

A big thanks to all who viewed my question but hadn't responded with the answer yet.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,519
Members
452,921
Latest member
BBQKING

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top