Reading Word Documents in WSH
“How can I read the contents of a Microsoft Word document using VBScript in WSH?”
Reading the contents of a Microsoft Word document in WSH is actually pretty simple. You can use any language you like with this method, but I’ll be using VBScript.
It only seems logical that the best way to read a Word document is to use the Word application. So that’s exactly what we’re going to do. With that being said, your script should start by defining a few necessary variables.
Const wdDoNotSaveChanges = 0 strDocument = "C:\mydoc.docx"
The most important variable here is the path to the Word document that you wish to read. This should be the full path. It can be any valid local or UNC path. The wdDoNotSaveChanges constant will be used to suppress the save changes dialog later.
Set objWord = CreateObject("Word.Application") objWord.Visible = False objWord.DisplayAlerts = False objWord.Documents.Open strDocument,, True Set objDoc = objWord.ActiveDocument
VBScript’s CreateObject method is used to create an instance of the Word automation object. This creates an instance in memory of the Word application. Setting the Word automation object’s Visible property to False will load the application in a hidden state. The DisplayAlerts property is then used to suppress any dialog boxes. Finally, the Word document is opened and activated.
Set objRange = objDoc.Content strContents = objWord.CleanString(objRange.Text)
Once the document is open, you must create a range containing the contents of the document. This can be done quite easily using the Document object’s Content property. Once you have range, the Text property is used to return the text contained within that range. Additionally, I’ve used the Word object’s CleanString method to remove any Word formatting from the text string.
objWord.Quit wdDoNotSaveChanges MsgBox strContents
As you can see, reading from a Word document in WSH is accomplished very easily using the Word automation object.
Be careful when using this method. Remember that you are reading the entire contents of the file. Larger files may exceed the allocated memory for your variable and cause problems. In these cases, you would be better to move through paragraph objects in your document. I’ll demonstrate that in a later article.
Please use the trackback link when linking to this post.
































