Scripting Microsoft Word - Editing and Formatting Documents
(Page 3 of 4 )
We’re now going to explore a few ways to edit documents. Please note that this section is only a very brief introduction. You will most likely need to do some experimentation with your own documents since editing is highly dependent upon the styles and formatting that exist within the document.
To begin, we’re going to assume that we’re working with our newly created blank document. We’ll first want to add a couple of paragraphs.
Set objSelection = objWord.Selection
We begin by selecting the contents of your document. In our new document, we’re simply creating an empty selection but we’ll need that selection reference to type our paragraphs.
objSelection.Font.Name = "Arial"
objSelection.Font.Size = "10"
objSelection.TypeText "=rand(1,5)"
objSelection.TypeParagraph()
objSelection.Font.Size = "12"
objSelection.TypeText "=rand(1,5)"
objSelection.TypeParagraph()
Adding text is quite simple. I’m using a few selection properties to control some basic formatting such as font face and size, and then typing the text to be inserted using the TypeText method. The TypeParagraph method is used to insert paragraph breaks between my text samples.
The TypeText method accepts a single attribute that should be the literal text to be included. Since I’m lazy (and I didn’t want to clog up my code with a bunch of lengthy paragraphs) I’ve used a little known Word function to insert some random sample text for me.
Depending on your Word version and regional settings, your document should now contain two paragraphs that look similar to the following:
On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look. You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab.
On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look. You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab.
If you’re using a Word version older than Word 2007 you will probably see something more like this:
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
In any case, we have a few sample paragraphs to work with now. Let’s take a look at how we can append paragraphs to a Word document and how to insert them between other paragraphs. To do both of these, we’ll need to change our selection.
Const wdStory = 6
Const wdMove = 0
Set objSelection = objWord.Selection
objSelection.EndKey wdStory, wdMove
objSelection.Font.Size = "14"
objSelection.Font.Bold = True
objSelection.TypeText "=lorem(1,5)"
objSelection.TypeParagraph()
The code sample above will append a paragraph to the end of our document. Notice how I’ve reset my selection before changing anything. This ensures that the entire document's contents are selected.
Next I use the EndKey method. This method moves or extends a selection from the end of the specified unit. I’m telling Word to move my selection to the end of the entire story (the full document contents). I now have an empty selection immediately following the document's contents. Here, I just write a new paragraph as before.
Set objSelection = objWord.Selection
objSelection.HomeKey wdStory, wdMove
objSelection.Font.Size = "14"
objSelection.Font.Bold = True
objSelection.TypeText "=lorem(1,5)"
objSelection.TypeParagraph()
I use the same procedure to insert a paragraph at the beginning of my document as well. The only difference is that I’m using the HomeKey method instead of the EndKey method. This moves my selection to the beginning instead of the end.
Const wdParagraph = 4
Set objSelection = objWord.Selection.Paragraphs(2)
objSelection.EndKey wdParagraph, wdMove
objSelection.Font.Size = "14"
objSelection.Font.Bold = True
objSelection.TypeText "=lorem(1,5)"
objSelection.TypeParagraph()
To insert paragraphs we use the same two procedures. We can iterate through the paragraphs collection to select single paragraphs within the document. Once we have a single paragraph selected, we can simply type a new paragraph either before or after it. The code sample above inserts a paragraph between our two original paragraphs.
I’ve done this by selecting the second paragraph in the document and then inserting a paragraph after it. Notice that I’m now using the wdParagraph unit size with the EndKey method because I want to move to the end of the current paragraph rather than the end of the current document. You can view all of the available unit size constants in the MSDN documentation here.
Next: Closing, Saving, and Exiting >>
More Windows Scripting Articles
More By Nilpo