Working with Object Methods

An object's properties describe what the object is, whereas its methods describe what you can do with the object. For example, in Word you can spell check a Document object by using the CheckSpelling method. Similarly, you can sort a Table object by using the Sort method.

How you refer to a method depends on whether or not the method uses any arguments. If it doesn't, the syntax is similar to that of properties:


For example, the following statement saves the active document: ActiveDocument.Save

If the method requires arguments, you use the following syntax: Object.Method (argument1, argument2, ...)

¡^ Technically,the parentheses around the argument list are necessary only if you'll be storing the ® result of the method in a variable or object property:

variable = Object.Method (argument1, argument2, ...)

For example, Word's Document object has a Close method that you can use to close a document programmatically. Here's the syntax:

Object.Close(SaveChanges, OriginalFormat, RouteDocument)

Object The Document object you want to work with.

SaveChanges A constant that specifies whether or not the file is saved before closing.

OriginalFormat A constant that specifies whether or not the file is saved in its original format.

RouteDocument A True or False value that specifies whether or not the docu ment is routed to the next recipient.

For example, the following statement prompts the user to save changes, saves the changes

(if applicable) in the original file format, and routes the document to the next recipient:

ActiveDocument.Close wdPromptToSaveChanges, wdOriginalFormat, True

For many VBA methods, not all the arguments are required. For the Close method,for example, only the SaveChanges argument is required.Throughout this book, I differentiate between required and optional arguments by displaying the required arguments in bold type. To skip a non-required argument (and thus use its default value),leave it blank,although you still need to enter all the commas that separate the arguments. For example,to exclude the OriginalFormat argument in the Close method,you use a statement like this: ActiveDocument.Close wdPromptToSaveChanges, , True

To make your methods clearer to read, you can use VBAs predefined named arguments. For example, the syntax of the Close method has three named arguments: SaveChanges, OriginalFormat, and RouteDocument. Here's how you would use them in the preceding example:

ActiveDocument.Close SaveChanges:=wdPromptToSaveChanges, _ OrignalFormat:=wdOriginalFormat, _ RouteDocument:=True

Notice how the := operator assigns values to the named arguments.

Named arguments make your code easier read, but they also bring two other advantages to the table:

■ You can enter the arguments in any order you like.

■ You can ignore any arguments you don't need (except arguments that are required by the method, of course).

^ How did I know to use the constants wdPromptToSaveChanges and wdOriginalFormat in the Close method example? They're all listed in the VBA Help system.To see them,type the method name in a module and then press Fl.The Visual Basic Editor launches the Help system and displays the Help topic for the method.

0 0

Post a comment