Word| Object Microsoft Word Object
Directory
Using Application objects
Use the Document Object
Use Range Object
Using Selection objects
Using Find and Replacement objects
Use table, Column, Row, and cell objects
Use other normal objects
Determine if an object is valid
Modify Word command
Use of events
Using automatic macros
Using automation
Visual Basic supports a collection of objects that correspond directly to the elements in Microsoft Word 97, and are familiar to the vast majority of these elements through the user interface. For example, the Document object represents an open file, and the Bookmark object represents a bookmark in a document, while the Selection object represents the selection in a document window pane. In Word, each class of elements-documents, tables, paragraphs, bookmarks, fields, and so on-can be represented by objects in Visual Basic. To automate tasks in Word, you can use the methods and properties of these objects.
For general content on understanding and using the Microsoft Office 97 object model, see the second chapter of this book, "Understanding the object model." The object model in Microsoft Office 97 is quite rich and contains about 180 objects. To view a hierarchical diagram of the Word object model, see "Microsoft Word Objects" in Help. To get a detailed description of a particular object, you can click the object's name in the diagram, or search for a specific object in the index of Help.
How to display word Visual basic Help
To use Word Visual Basic Help, you must select a custom installation during Setup, and the Visual Basic online Help check box is selected for Word. Otherwise, the Setup program does not install Visual Basic Help. If the user has already installed Word, you can run the Setup program again to install Visual Basic Help.
To view word Visual basic Help, you can click the "Table of Contents and index" item in the Help menu in the Visual Basic Editor. In the table of contents in the Help Topics dialog box, double-click Microsoft Word Visual Basic Reference, and then double-click Visual Basic Reference. The Help Topics dialog box appears again, showing the contents and index of Microsoft Word Visual Basic.
Using Application objects
A Application object is also created while the user starts a Word window. Users can use the properties or methods of the Application object to control or return application-scoped attributes, control the appearance of the application window, or adjust other aspects of the Word object model. You can use the Application property to return a Word application object. The following example switches from view state to print preview state.
Application. PrintPreview = True
Some properties of the Application object control the appearance of the application. For example, if the DisplayStatusBar property is True (true), the status bar is visible, and if the WindowState property value is wdWindowStateMaximize, the application window is maximized. The following example sets the size of the application window on the screen.
With application
. WindowState = wdWindowStateNormal
. Height = 450
. Width = 600
End With
The properties of the Application object can also access lower-level objects in the object hierarchy, such as Windows collection (which represents all currently open windows) and the Documents collection (representing all currently open documents). Users can use attributes, sometimes called accessors, to access lower-level objects (Document objects, window objects, selection objects, and so on) from the Application object at the highest level in the object hierarchy. You can use one of the following two examples to open mydoc.doc.
Application. Documents. Open FileName: = "C:\DOCS\MYDOC." DOC "
Documents. Open FileName: = "C:\DOCS\MYDOC." DOC "
Because the document property is common, the Application property is optional. Common properties and methods do not need to application the object qualifier. To browse the list of shared properties and methods in the Object Browser, you can click < shared > in the Categories column. A total of initializes is listed in the Members column.
Note The option object contains some properties that control the behavior of Word sharing. The properties of many option objects correspond to the items in the Option dialog box (the Tools menu). You can return the option object by using the option property of the Application object. The following example sets three application-wide options (because the option property is common, so no application property is required in this example).
With Application.options
. AllowDragAndDrop = True
. ConfirmConversions = False
. MeasurementUnit = wdpoints
End With
Use the Document Object
A Document object is also created when the user opens or creates a file in Word. Users can use the properties or methods of the Document object or the Documents collection to open, create, save, activate, or close a file.
Return to Document Object
The user can use the grammar documents (index) to return any open document as a Document object, where index is the name or index number. In the following example, the variable mydoc contains a Document object that represents the open documentation named "Report.doc."
Set MyDoc = Documents ("Report.doc")
The index number represents the location of the document in the Documents collection. In the following example, the variable mydoc contains a Document object that represents the first document in the Documents collection.
Set MyDoc = Documents (1)
Note Because the index number of a particular document changes when a user adds or closes multiple documents, it is a good idea to use the name of the document to index the documents object in the Documents collection.
In addition to referencing documents by name or index number of the document, you can also use the ActiveDocument property to return a Document object that references the active document (the document currently in focus). The following example displays the name of the active document, and if there is no document open, the example displays a message.
If Documents. Count >= 1 Then
MsgBox ActiveDocument. Name
Else
MsgBox "No documents are open"
End If
Open Document
To open a document that already exists, you can use the Open method. The Open method applies to the Documents collection returned through the document property. The following example opens the file Test.doc (from the current folder) and initiates change tracking.
Set MyDoc = Documents. Open (FileName: = "TEST. DOC ")
MyDoc. TrackRevisions = True
Note that the return value of the Open method in the previous example is a Document object that represents the newly opened documents. The file name in this example does not contain a path, so it is assumed to be in the current folder. However, this can cause a run-time error because Visual Basic will no longer find the file if the user creates a different folder than the current folder. However, the user can make sure that the correct file is open by indicating the full path, as shown in the following table.
Operating system
FileName parameter
Windows
FileName: = "C:\Documents\Temporary File.doc"
Macintosh
FileName: = "Hard Drive:Documents:Temporary File"
If the user's macros are used only for one file system, you can specify the path separator ("\" or ":") in the filename argument, as shown in the previous table. The following example shows a file system-independent code that can be used to open Sales.doc and assumes that Sales.doc has been saved in a program folder in Word.
Programpath = Options. DefaultFilePath (Wdprogrampath)
Documents. Open FileName: =programpath & Application. PathSeparator & "SALES. DOC "
The PathSeparator property returns the correct file separator for the current file system (for example, the file allocation table used for ms-dos/windows, or ":" for the Macintosh). The DefaultFilePath property returns the location of the file, such as the path to the folder where the document is located, the program folder, or the current folder.
An error occurs if the specified file name does not appear in the current folder (if no path is specified) or if it appears in the specified folder (if the path is specified). The following example uses the properties and methods of the FileSearch object to determine whether a file named "Test.doc" exists in the user's default document folder. If the file was found (FoundFiles. Count = 1), then open it;
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] Next page >>