Working with Object Methods

     

Working with Object Methods

An object'sproperties 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 using the CheckSpelling method. Similarly, you can sort a Table object by using the Sort method.

graphics/note_icon.gif

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, ...) 

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:

  Object.Method  

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,  ...) 

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 document is routed to the next recipient.

graphics/note_icon.gif

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 

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 

To make your methods clearer to read, you can use VBA's 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 named arguments are assigned values by using the := operator.

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 necessary arguments, of course).

tip

graphics/tip_icon.gif

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 F1. The Visual Basic Editor launches the Help system and displays the Help topic for the method.




Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net