Advanced Word Object Scripting
(Page 1 of 4 )
In my last two articles I’ve demonstrated ways of using Microsoft Word through its OLE automation object. In this article, I’m going to show you a few more advanced scripting techniques that you can use. The first two are best used for data mining or mass-file editing while the last is a way of exploiting some of Word’s functionality for use in other environments.We’ll begin by taking a look at Word’s Find and Replace features. These features allow you to search a Word document for words or phrases and replace them. You’ve more than likely used these features within the Word application so I’m not going to go into any more detail describing them. Instead, let’s learn how to script them!
For now, we’ll focus on the Find feature. This can be useful for several different purposes. Perhaps you want to mine some data from a Word document. The Find feature would allow you to find that data quickly and efficiently. Or maybe you have a folder full of documents and you're looking for a quick way to identify which ones pertain to a specific topic. Whatever your purpose might be, scripting the Find feature could prove to be useful.
Const wdFindContinue = 1
strFindText = "Text to find"
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open("mydoc.doc")
As we’ve done before, we’ll begin our script by setting a few constants and variables, creating an instance of the Word application, and opening a document. The strFindText variable should contain the text string that you wish to find in your Word document.
Set objSelection = objWord.Selection
Next we’ll want to create a selection containing the entire contents of the Word document. This allows us to search the entire document. Want to limit your search to a specific part of the document? That’s fine too. In that case, set your selection to only the parts of the document you wish to search. This could be a single page or even a single paragraph.
Next: Finding Text in a Word Document >>
More Windows Scripting Articles
More By Nilpo