Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
|
|
The Documents collection, available from the Application object's Documents property, contains a collection of Document objects open in Word. It also has methods used to access a Document in the collection, create a new document, open an existing document, close all the documents, and save all the documents. Iterating over the Open Documents
The Documents collection can be iterated over using a For Each loop in Visual Basic 2005. Listing 8.18 shows a simple example of iterating over the open documents in Word and printing the name of each document to the console. Listing 8.18. Iterating over the Documents Collection Using For Each
Accessing a Document in the Documents Collection
To access a Document in the Documents collection, you use the Item property, which returns a Document object. The Item property has an Index parameter passed by reference that is of type Object. You can pass an Integer representing the 1-based index of the document in the collection you want to access. Alternatively, you can pass a String representing the name of the document you want to access. The name you pass for a document is the full name of the file, if it has been saved (for example, "c:\Documents and Settings\John\Desktop\Doc1.doc"). If the document has not yet been saved, the name to pass is the temporary name that Word creates for a new document. This temporary name is typically something like Document1, with no file extension. Listing 8.19 shows an example of calling Item with a 1-based index and a String index. Listing 8.19. A VSTO Customization That Uses Item to Get a Document
You can also use the Count property to determine the number of open documents. You should check the Count property before accessing a document by index. Creating a New Document
To create a new document, you can use the Documents collection's Add method. The Add method returns the newly created Document object. It takes four optional by reference parameters of type Object, as described in Table 8.5.
Opening an Existing Document
To open an existing document, use the Documents collection's Open method, which returns the opened Document object. The Open method takes one required Object parameter, to which you pass the String representing the filename to open. The Open method also takes 15 optional by reference parameters of type Object, as described in Table 8.6.
Listing 8.20 shows the simplest possible way to call the Open method to open a document. Listing 8.20. A VSTO Customization That Uses the Open Method to Open a Document
Closing All Open Documents
The Close method on the Documents collection closes all the open documents in Word. It takes three optional parameters of type Object by reference. The first optional parameter, called SaveChanges, is of type Object and can be passed a member of the WdSaveOptions enumeration: wdDoNotSaveChanges, wdPromptToSaveChanges, or wdSaveChanges. The second optional parameter, called OriginalFormat, is of type Object and can be passed a member of the WdOriginalFormat enumeration. The second parameter controls Word's behavior when saving a changed document whose original format was not Word document format. This parameter can be passed wdOriginalDocumentFormat, wdPromptUser, or wdWordDocument. The final optional parameter is called RouteDocument and is of type Object. Passing true for this parameter routes the document to the next recipient if a routing slip is attached. It is also possible to close an individual document using the Document object's Close method, as discussed later in this chapter. You have already learned how to use the Application object's Quit method as a third way to close all open documents and quit Word. The Quit method takes the same parameters as Documents.Close and Document.Close. Saving All Open Documents
The Save method on the Documents collection saves all the open documents in Word. It takes two optional parameters. The first optional parameter, called NoPrompt, is of type Object and can be set to true to have Word automatically save all open documents without prompting the user. The second optional parameter, called OriginalFormat, is of type Object and can be passed a member of the WdOriginalFormat enumeration. The second parameter controls Word's behavior when saving a changed document whose original format was not Word document format. It is also possible to save an individual document using the Document object's Save or SaveAs method, as discussed later in this chapter. |
|
|