Programming Basics for developing Word documents using VBA

Source: Internet
Author: User
Tags numeric value

① what are objects and collections

An object represents a Word element, such as a document, paragraph, bookmark, or individual character. A collection is also an object that contains multiple other objects, usually of the same type; for example, a collection object can contain all the bookmark objects in the document. By using properties and methods, you can modify individual objects, or you can modify the entire collection of objects.

② what is a property

A property is an attribute of an object or an aspect of the object's behavior. For example, a document property contains its name, content, save status, and whether tracked changes are enabled. To change the characteristics of an object, you can modify its property values.

To set the value of a property, you can immediately follow the object with a period, a property name, an equal sign, and a new property value. The following example enables tracked changes in a document named "MyDocument.doc."

Sub Trackchanges ()

Documents ("Sales.doc"). TrackRevisions = True

End Sub

In this example, documents refers to a collection of open documents, while "MyDocument.doc" identifies a separate document in the collection. and set the TrackRevisions property of the document.

Some properties cannot be set. The property's Help topic indicates that the property can be set (read-write) or read only (read-only).

You can get information about the object by returning one of its property values. The following example returns the name of the active document.

Sub GetDocumentName ()

Dim strDocName as String

strDocName = ActiveDocument.Name

MsgBox strDocName

End Sub

In this example, activedocument refers to the document in the Word Activity window. The name of the document is assigned to the strDocName variable.

Description

The Help topic for each property indicates that the property can be set (read-write), read only, or write only the property (write only). In addition, the Object Browser in the Visual Basic editor displays the read-write status of the selected property at the bottom of the Browse window.

③ what is the method

Method is an action that an object can perform. For example, the Document object has a PrintOut method as long as it can be printed. Methods usually have parameters to limit how the action is performed. The following example prints the first three pages of the active document.

Sub Printthreepages ()

ActiveDocument.PrintOut range:=wdprintrangeofpages, pages:= "1-3"

End Sub

In most cases, the method is an action, and the attribute is a property. Using a method causes some events to occur for the object, while using the property returns the object's information or causes a change in the object's properties.

④ returns an object

You can return most objects by returning a separate object in the collection. For example, the Documents collection contains open Word documents. You can return the Documents collection by using the Documents property of the Application object (located at the top of the Word object structure).

After you have accessed the collection, you can return a separate object by using the index ordinal in parentheses (similar to how the array is handled). The index number is usually a numeric value or a name. For more information, see returning an object from a collection.

The following example uses the Documents property to access the Documents collection. The index number is used to return the first document in the Documents collection. The Close method is then applied to the Document object, and the first documents in the Documents collection are closed.

Sub closedocument ()

Documents (1). Close

End Sub

The following example uses a name (specified as a string) to identify the document object in the Documents collection.

Sub Closesalesdoc ()

Documents ("Sales.doc"). Close

End Sub

Collection objects typically have methods and properties that you can use to modify the entire collection of objects. The Documents object has a Save method that you can use to save all the documents in the collection. The following example saves all open documents by using the Save method.

Sub saveallopendocuments ()

Documents.save

End Sub

The Document object can also use the Save method to save separate documents. The following example saves a document named Sales.doc.

Sub Savesalesdoc ()

Documents ("Sales.doc"). Save

End Sub

To return an object that is at the bottom of the Word object structure, you must "drill down" to the object by using the properties and methods that can return the object.

To see the execution of the procedure, open the Visual Basic Editor, and on the View menu, click Object Browser. Click Application in the Class list on the left. Then click ActiveDocument in the members list on the right. The text is displayed at the bottom of the Object Browser, indicating that activedocument is read-only and returns the Document object. Then click Document at the bottom of the Object Browser, the Document object is automatically selected in the Classes list, and the document object's members are displayed in the members list. Scroll through the Members list, find Close, and click the Close method. The text is displayed at the bottom of the Object Browser window, explaining the syntax of the method. For more information about the method, press F1 or click the Help button to jump to the help topic for the Close method.

Based on this information, you can write the following directives to close the active document.

Sub Closedocsavechanges ()

Activedocument.close savechanges:=wdsavechanges

End Sub

The following example maximizes the active document window.

Sub Maximizedocumentwindow ()

ActiveDocument.ActiveWindow.WindowState = wdWindowStateMaximize

End Sub

The ActiveWindow property returns a Window object that represents the active window. Set the WindowState property to the maximum constant (wdwindowstatemaximize).

The following example creates a new document and displays the Save As dialog box, which provides a name for the document.

Sub createsavenewdocument ()

Documents.Add.Save

End Sub

The Documents property returns the Documents collection. The Add method creates a new document and returns a text object. Then apply the Save method to the Document object.

As shown above, you can use a method or property to access the underlying object. That is, in an object structure, you can return a method or property to an object's upper-level object by applying it to it. Once you have returned the desired object, you can apply the method of the object and control its properties.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.