How Can I Save Word Documents as Text Files By Using a Script?

How Can I Save Word Documents as Text Files By Using a Script?

  • Comments 7
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a series of Word files. I would like to open each of these files and save them as plain-text files. Is there a way to do that using a script?

-- CG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CG. Yes, you can do this quite easily with a script; in fact, with very few exceptions anything you can do within a Microsoft Office program you can do by using a script. Within Word, you can do a File - Save As and save a Word document as a text file. And you can do the very same thing using a script. In fact, you can do it using this script:

Const wdFormatText = 2

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("c:\scripts\mylog.doc")
objDoc.SaveAs "c:\scripts\mylog.txt", wdFormatText

objWord.Quit

We start off by creating a constant named wdFormatText and assigning it the value 2; this constant will be used to tell Word we want our new file saved as a text file. We then create an instance of Microsoft Word, and use the Open method to open the file C:\Scripts\MyLog.doc. After the document is open, we need just one line of code to save the Word document as a text file:

objDoc.SaveAs "c:\scripts\mylog.txt", wdFormatText

As you can see, we call the SaveAs method, and we pass it two parameters: 1) C:\Scripts\MyLog.txt, which is the path for our new text file; and, 2) wdFormatText, which tells Word to save the file as plain text. That’s it. We then use the Quit method to close our instance of Word.

Two quick notes. First, you won’t see any of this happening on screen; that’s because, by default, Word runs in an invisible window any time you call it from a script. If you’d prefer to see it pop up on the screen, save the file, and then disappear, use this code instead:

Const wdFormatText = 2

Set objWord = CreateObject("Word.Application")
objWord.Visible = TRUE
Set objDoc = objWord.Documents.Open("c:\scripts\mylog.doc")
objDoc.SaveAs "c:\scripts\mylog.txt", wdFormatText

objWord.Quit

The only difference here is that we’ve set the Visible property to TRUE.

Second, you can save your Word documents in formats other than plain text. For example, to save a Word document as an HTML file, use the constant wdFormatHTML (value = 8); to save a Word document as XML, use the constant wdFormatXML (value = 11).

And at the risk of sounding like an informercial, if you’d like to know more about scripting and Microsoft Office you might want to check out the Scripting Week 2 webcast If You Want Something Done Right, Let Microsoft Office Do It For You.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Guys, the script above is great for a single word doc. I have about 600 word docs I need to open and then save as .txt with the same name as the word doc. any ideas?

    DB

  • Hi,

    Above Script is very useful.

    Many Thanks

  • Hi there script guy. Just been learning by running the scripts here and everyttime i run the script i get a Run-Time  Error 4198 Command Failed.

    Any idea why .?

  • how to create a word document from the txt file?

  • Hi ,

    I have the same issue as DaveB.! Any solutions ?

    SN

  • xlCurrentPlatformText -4158 Current Platform Text
    xlWorkbookNormal -4143 Workbook normal
    so maybe the -4198 error means format you are trying to save the file to?

  • thanks