Microsoft Office Tutorials and References
In Depth Information
Object methods
Object methods
In addition to properties, objects also have methods. A method is an action that is
performed with an object. Here’s a simple example that uses the PrintPreview
method of a Chart object. When you execute this procedure, the active chart is
displayed in Excel’s print preview window.
Sub PreviewChart()
ActiveChart.PrintPreview
End Sub
Most methods also use arguments to define the action further. Here’s an example
that uses the Export method of a Chart object to save the active chart to a GIF file.
The Export method uses two arguments (both strings), which represent the
filename and the type of file to export.
Sub ExportChart()
ActiveChart.Export “mychart.gif”, “GIF”
End Sub
Specifying Arguments for Methods and Properties
An issue that often leads to confusion among VBA programmers concerns arguments
for methods and properties. Some methods use arguments to further clarify the action
to be taken, and some properties use arguments to further specify the property value.
In some cases, one or more of the arguments is optional.
If a method uses arguments, place the arguments after the name of the method,
separated by commas. If the method uses optional arguments, you can insert blank
placeholders for the optional arguments. Consider the Protect method for a
workbook object. Check the online help and you’ll find that the Protect method
takes three arguments: password , structure , windows . These arguments
correspond to the options in the Protect Workbook dialog box.
If you want to protect a workbook named MyBook.xls, for example, you might use a
statement like this:
Workbooks(“MyBook.xls”).Protect “xyzzy”, True, False
In this case, the workbook is protected with a password (argument 1). Its structure is
protected (argument 2), but not its windows (argument 3).
If you don’t want to assign a password, you can use a statement like this:
Workbooks(“MyBook.xls”).Protect , True, False
Search JabSto ::




Custom Search