Microsoft Office Tutorials and References
In Depth Information
Working with Object Libraries
To find out more about a selected object, property, or method in the Object
Browser, click the Help icon — the yellow question mark — in the Object
Browser window (refer to Figure 1-2).
Using the Application object
Each application exposes its own set of objects to VBA, but one object that
all applications have in common is Application. The Application object
exposes a program’s objects, properties, and methods to VBA. When a program
is open, its objects are available to VBA. If VBA opens a Word document, for
example, everything in that Word document is also exposed. VBA can do
anything in the Word document that a human can do from the Word Ribbon.
To take control of an application, you first have to create an instance of the
application in VBA. Creating an instance is basically the same as opening the
program from the Windows Start menu. When you start Microsoft PowerPoint
on your computer, for example, you’re creating an instance of PowerPoint on
your computer.
To create an instance of an application in VBA, you have to declare a variable
that references that object. The variable can have any name you like, but
you should attempt to give it a meaningful name. The syntax for declaring an
object variable is
Dim objectVariable as New program .Application
For more information on declaring and using variables, see Book VIII,
Chapter 3.
The objectVariable in the preceding example is the name of the variable.
The program is a reference to one of the Office applications (such as Word,
Excel, or Outlook). The Application part refers to the Application
object of that program. The New keyword ensures that VBA creates a new
instance of the program. Here are some examples of declaring new instances
of the Office programs:
Dim XL as New Excel.Application
Dim Wrd as New Word.Application
Dim Olk as New Outlook.Application
Dim PPT as New PowerPoint.Application
After you declare the object variable for the desired program, you can control
that program. To take control of the program, you must open the program
from VBA. The syntax for opening a program in VBA is
Set objectVariable as CreateObject(“ program .Application”)
Search JabSto ::

Custom Search