Scripting Microsoft Word - Creating and Opening Documents
(Page 2 of 4 )
At this point, the Word application is running but we don’t have any open documents to work with. I’ll show you how to create a new document, open an existing one, and switch back and forth between multiple open documents. Let’s begin by creating a new document.
Set objNewDoc = objWord.Documents.Add
Here I’ve created a new document based on the Normal template. The most effective way to create a new document is by using the Add method of the Documents collection. The Documents collection is a collection of Document objects that represents all open documents.
Documents.Add(Template, NewTemplate, NewDocumentType, Visible)
You may provide optional attributes to the Add method. Template is a string that indicates a path to a document template to use. If omitted, the default Normal template is used. NewTemplate is a Boolean value that indicates whether or not to open the new document as a template. NewDocumentType accepts any one of the wdNewDocumentType constants listed in the table below. If omitted, the default wdNewBlankDocument (0) is used. Visible is a Boolean value that indicates whether or not the document should be opened in a visible window. This defaults to True but will be overridden if the Word object itself has visibility set to False.
wdNewDocumentType Constants |
wdNewBlankDocument = 0 |
wdNewWebPage = 1 |
wdNewEmailMessage = 2 |
wdNewFrameset = 3 |
wdNewXMLDocument = 4 |
The following example uses the Documents.Add method to open a new document using the Professional Memo template.
strTemplate = "C:Program FilesMicrosoft Office" _
& "TemplatesMemosProfessional Memo.dot"
Set objDoc = objWord.Documents.Add(strTemplate)
The Documents collection object also provides an Open method that you can use to open an existing document. The Open method provides very extensive syntax; however, it only requires a single attribute that represents the path to the file to be opened. Each of the attributes may also be set using equivalent properties provided by the Documents object. I’ll show you both ways and you’ll understand what I mean.
For brevity’s sake, I won’t be discussing all of the available attributes since most will go unused. For more information on the Open method and its available attributes, consult the MSDN documentation here.
Documents.Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument, OpenAndRepair, DocumentDirection, NoEncodingDialog)
For most purposes, we’ll only be concerned with the first and third attributes. FileName is a string that represents the path to the document file. This may be a local path or any fully qualified UNC path to a network file. You can even use URLs.
If you are using Jscript, don’t forget to escape any and all forward slashes in your paths!
The third attribute is optional and accepts a Boolean value indicating whether or not to open the document as Read-Only. This can be used as an added safety precaution. For example, you may want to print out a document with some changes without affecting the original document.
Set objDoc = objWord.Documents.Open("MyDoc.doc",, True)
The previous example opens the document MyDoc as Read-Only. Any changes made to the document will not be saved to the original. We’ll see later how to save a document in case you would like to save the changes to a new document.
Now that you know how to open new and existing documents, you need to know how to switch back and forth between them. The Document object provides an Activate method that allows you to specify which document has focus. The Word Application object also provides a read-only ActiveDocument property that returns a reference to the currently active document.
Be aware that the ActiveDocument property will result in an error if there are no documents open. A simple If statement can be used to prevent this.
In the following example, we’re going to switch between our two open documents. The newly opened document should have focus already. We’ll switch back to the new document we created. I’ll use a message box to indicate that the switch was made. Notice how I use a simple If statement to check for open documents in order to avoid any errors. I’m assuming that if there is at least one document open, then both required documents are open.
If objWord.Documents.Count >= 1 Then
objNewDoc.Activate
MsgBox "The " & objNewDoc.Name & " document has been activated. "
Else
MsgBox "There are no documents open."
End If
Here, we’re using the Activate method to focus on the objNewDoc object. We’re checking the Count property of the Documents collection to ensure that it contains references to open documents.
The Word Application object also provides an Activate method that can be used to switch between multiple running instances of the Word application!
Next: Editing and Formatting Documents >>
More Windows Scripting Articles
More By Nilpo