Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
The Document object represents an open document in Word. The Document object has a Name property that returns a String representing the name of the document (for example, "doc1.doc"). If the document has not yet been saved, this property returns the temporary name of the documenttypically, something like Document1. Document also has a FullName property that returns a String representing the full filename of the document if the document has been saved. Once again, if the document has not been saved, this property returns the temporary name of the document, such as Document1. The FullName of the document can be passed to the Item property of the Documents collection to access the document by name from that collection. The Path property returns a String representing the path to the folder where the document is stored. A document with FullName "c:\mydocuments\doc1.doc" returns "c:\mydocuments" for the Path property, for example. If the document has not yet been saved, the Path returns an empty string. The Type property is of type WdDocumentType and can be used to determine whether the document is a Word document or a Word template file. A Word document returns the enumerated value wdTypeDocument. A template returns the value wdTypeTemplate. Preserving the Dirty State of a Document
Saved is a Boolean property that tells you whether a document needs to be saved. A document that has not been changed, such as a new document that has not been typed in yet or a document that has been opened but not edited, returns TRue for Saved. A document that has been changed returns False until the user or code saves the document and thereby resets the Saved property to true. A document that has been changed but not saved is often referred to as a dirty document. You can also set the value of the Saved property so that a change made by your code does not dirty the document. You might make a change through code to a document, for example, but you do not want to save the change made by your code unless the user makes some additional change to the document. This is often desirable because when users open a document and do not edit it, they are confused when they are prompted to save because code associated with the document changed the state of the document in some way. You can get the value of the Saved property, make the change to the document, and then set the value of Saved back, as shown in Listing 8.21. Listing 8.21. A VSTO Customization That Preserves the Dirty State of the Document by Using the Saved Property
Closing and Saving a Document
The Close method enables you to close a document. The Close method takes three optional Object parameters passed 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. The Save method saves the document and does the same thing that choosing Save from the File menu would do. If the document has already been saved, it saves the document to the location it was last saved to. If the document has not yet been saved, it brings up the Save As dialog box so that the user can select a place to save the document. The SaveAs method takes 16 optional Object parameters passed by reference. It gives you full control over the filename to save to, as well as the file format and several other options. Table 8.7 lists the optional parameters of type Object that are passed by reference to the SaveAs method.
Working with Windows Associated with a Document
A particular document can have one or more windows associated with it. Even when a document is opened with False passed to the Visible parameter of the Documents collection's Open method, it still has a window associated with it, albeit a window whose Visible property is False. When a document has multiple windows associated with it, you can use the Windows property to return the collection of windows associated with that document. You can determine which of the windows will have the focus when the document is active by using the ActiveWindow property. To activate a particular document and make its ActiveWindow the one with focus, use the Activate method. Changing the Template Attached to a Document
A document always has a template associated with it. By default, the template is the Normal template (normal.dot), also available from the Application object's NormalTemplate property. A document might be associated with some other template, usually because it was created from a particular template. If you have a Document object and you want to determine what template is associated with it, you can use the AttachedTemplate property. When you get the value of AttachedTemplate, it returns an Object that you can cast to a Template object. When you set the value of AttachedTemplate, you can pass either a Template object or a String containing the filename of the template. Important Collections Associated with Both Document and Range
The Document and Range objects share a number of properties that return collections you will frequently use. Rather than consider these properties both in this section and in the section on Range later in this chapter, we cover both of them here only. Table 8.8 shows these properties associated with both Range and Document that return important collection objects.
Note that the Characters, Sentences, and Words collections are special collections that return Range objects when you iterate over them. Listing 8.22 shows a VSTO customization that uses these collections, as well as the Paragraphs collection. It creates a document with some text in it and then a second document to output information about the first document. Listing 8.22. A VSTO Customization That Uses the Characters, Paragraphs, Sentences, and Words Collections
Important Collections Associated with Document Only
Some properties return collections associated only with Document, not with Range. Table 8.9 shows several of these properties.
Working with Document Properties
Document has a BuiltinDocumentProperties property that returns an Object that can be cast using CType to an Office.DocumentProperties collection representing the built-in document properties associated with the document. These are the properties that you see when you choose Properties from the File menu and click the Summary tab. These include properties such as Title, Subject, Author, and Company. Table 8.10 shows the names of all the document properties associated with a document.
Document also has a CustomDocumentProperties property that returns an Object that can be cast to an Office.DocumentProperties collection representing any custom document properties associated with the document. These are the custom properties that you see when you choose Properties from the File menu and click the Custom tab. Custom properties can be created by your code and used to store name-and-value pairs in the document. The DocumentProperties collection and DocumentProperty object are located in the Microsoft Office 11.0 Object Library (office.dll), which contains objects shared by all the Office applications. These objects are in the Microsoft.Office.Core namespace and typically are brought into Office projects in an Office namespace as shown here: Imports Office = Microsoft.Office.Core
Listing 8.23 shows an example of iterating over the DocumentProperties collection returned by the CustomDocumentProperties and BuiltInDocumentProperties properties. We get the value of the built-in properties in a try/Catch block because some built-in properties throw exceptions when their values are accessed. Listing 8.23. A VSTO Customization That Iterates over DocumentProperties Collections
To access a DocumentProperty in a DocumentProperties collection, you use the indexing syntax (docProperties(Object)), which returns a DocumentProperty object. The indexer takes an Index parameter of type Object. You can pass an Integer representing the 1-based index of the DocumentProperty in the collection you want to access. Alternatively, you can pass a String representing the name of the DocumentProperty you want to access. As with other collections, the Count property returns how many DocumentProperty objects are in the collection. A DocumentProperty object has a Name property that returns a String containing the name of the property. It also has a Value property of type Object that returns the value of the property. You can check what the type is of Value by using the Type property that returns a member of the Office.MsoDocProperties enumeration: msoPropertyTypeBoolean, msoPropertyTypeDate, msoPropertyTypeFloat, msoPropertyTypeNumber, or msoPropertyTypeString. Listing 8.24 shows how a DocumentProperty is accessed. Listing 8.24. A VSTO Customization That Accesses a DocumentProperty Using an Indexer
You can add a custom DocumentProperty to a DocumentProperties collection by using the Add method. The Add method takes the parameters shown in Table 8.11.
Listing 8.25 shows an example of adding a custom DocumentProperty of type msoPropertyTypeString. Note that Word will let you set the value to a long String, but it will truncate that value to 255 characters. Fortunately, VSTO enables developers to store larger amounts of data in a document through a feature called cached data. For more information on the cached-data feature of VSTO, see Chapter 18, "Server Data Scenarios." Listing 8.25. A VSTO Customization That Adds a Custom DocumentProperty
Checking Spelling and Grammar in Documents and Ranges
You can control the grammar checking in a Document or Range by using the following methods and properties. GrammarChecked is a Boolean property that returns TRue if the grammar in the document or range has been checked. If the grammar has not yet been checked, you can force a grammar check by calling the CheckGrammar method. You can control whether Word shows the grammatical errors in the document by setting the ShowGrammaticalErrors property to TRue or False. The GrammaticalErrors property returns a ProofreadingErrors collection, which is a collection of Range objects containing the ranges of grammatically incorrect text. A similar set of methods and properties exists for checking spelling. SpellingChecked is a Boolean property that returns true if the spelling in the document or range has been checked. If the spelling has not yet been checked, you can force a spelling check by calling the CheckSpelling method. The CheckSpelling takes 12 optional Object parameters passed by reference that you can omit unless you want to specify additional custom dictionaries to check the spelling against. You can control whether Word shows the spelling errors in the document by setting the ShowSpellingErrors property to true or False. The SpellingErrors property returns a ProofreadingErrors collection, which is a collection of Range objects containing the ranges of incorrectly spelled text. Listing 8.26 shows an example that uses many of these properties and methods. Listing 8.26. A VSTO Customization That Checks Grammar and Spelling
Printing a Document
The Document object has a PageSetup property that returns a PageSetup object that has several properties for configuring the printing of a document. The PrintOut method can be used to print a document. It has 18 optional Object parameters passed by reference. Table 8.12 lists some of the most commonly used optional parameters for PrintOut.
Listing 8.27 shows a simple example that sets some page-margin options using the PageSetup property and then calls PrintOut specifying that two copies be printed. Listing 8.27. A VSTO Customization That Uses the PrintOut Method
Working with Document Protection
Document protection enables you to protect a Word document so the document can be edited only in certain ways by certain people. Document protection in Word works on the principle of exclusions; you first protect the whole document as read-only and then mark certain areas of the document as exclusions. This allows your users to edit only the parts of the document that you specify as exclusions. Figure 8.4 shows the Protect Document task pane that is shown when you choose Protect Document from the Tools menu. The Allow Only This Type of Editing in the Document check box has been checked, and the drop-down list has been set to not allow any changes. You can optionally allow users to make comments in the document, fill out forms, or make tracked changes to the document. Figure 8.4. The Protect Document task pane.
Given a basic protection level for the document, you can then add some exceptions by selecting the parts of the document that should be editable and checking either a Groups or Individuals check box to allow that group or individual to edit the selection. Word always provides an Everyone group, but you can add groups and individuals by clicking the More Users link in the task pane. Clicking this link brings up a dialog box that lets you enter a Windows username (DOMAIN\username), Windows user group (DOMAIN\usergroup), or e-mail address. After you have selected the parts of the document you want to be exceptions and checked the check box next to the groups or individuals you want to be able to edit those parts of the document, click the Yes, Start Enforcing Protection button to protect the document to bring up the Start Enforcing Protection dialog box, shown in Figure 8.5. Word prompts you for an optional password if you want to require a password to remove the document protection. Word can also use user authentication to protect and encrypt the document to protect it further. Figure 8.5. The Start Enforcing Protection dialog box.
With protection enforced, Word highlights the area of the document that you are allowed to edit based on the exception set for the document. Figure 8.6 shows a document that has been protected but has the first sentence as an editing exception for the Everyone group. Word highlights the regions that you are allowed to edit in the document and provides a task pane for navigating between regions you are allowed to edit. Figure 8.6. A document with protection enforced but with an exception to allow editing of the first sentence.
Document-protection settings apply to code that is talking to the Word object model, too. If the user is not allowed to edit any sentence but the first sentence, code is also restricted to being able to change only the first sentence. If you run code that tries to change protected parts of the document, an exception is raised. Word provides several properties and methods that enable you to protect the document programmatically and examine protection settings, as listed in Table 8.13.
Working with Password Protection
In addition to a password that may be associated with document protection, a Word document can have a password that must be entered to open the document. It can also have a second password associated with it that must be entered to modify or write to the document. These passwords can be set by choosing the Tools menu in the Save As dialog box and picking Security Options. Figure 8.7 shows the Security dialog box. Figure 8.7. The Security dialog box.
The Document object's HasPassword property returns TRue if the document has been protected with a password that must be entered to open the document. The Password property is a write-only property that can be set to a String value representing the password for the document. Word also has the notion of a password to allow the user to modify or write to the document. If the WriteReserved property returns true, the document has been protected with a password that must be entered to modify or write to the document. The WritePassword property is a write-only property that can be set to a String value representing the write and modify password for the document. Undo and Redo
Unlike Excel, Word adds the changes you make with your code to the undo stack. You can undo and redo actions your code or a user has taken using the Document object's Undo and Redo methods. Both methods take by reference an optional object parameter that you can set to the number of undo steps or redo steps you want to take. The UndoClear method clears the undo stack, making it so the user can neither undo nor redo any recent actions. |
Категории