Writing Automation Code


To work with Office documents (such as Word documents) in VBA code, you need to write Automation code. Automation (previously called OLE—Object Linking and Embedding—or OLE Automation) lets you work with the object models of Office applications (and some non-Office applications). The object models represent the objects, properties, methods, and events of that program in a form that can be used in code. When working with Word, this means that you can work with documents, styles, ranges, bookmarks, and just about any other components you can manipulate in the Word interface.

The first step in writing Automation code is to set a reference to the appropriate object model, in this case Word. Open an Access module, drop down the Tools menu in the Visual Basic window, and select References. The References dialog opens, with the currently selected references checked. By default, a brand-new Access 2000 or higher database has only the references shown in Figure 11.1.

click to expand
Figure 11.1

Some Handy Toolbar Buttons

To save time when working with modules and references, I suggest placing a Visual Basic Editor button on the main Access toolbar and a References button on the Visual Basic window toolbar. To place a Visual Basic Editor button on the main Access toolbar, follow these steps:

  1. With the database window visible, right-click the grey background of the Database toolbar and select Customize from the context menu, as shown in Figure 11.2.


    Figure 11.2

  2. In the Customize dialog, select Tools in the Categories well, and locate the Visual Basic Editor command in the Commands well, as shown in Figure 11.3.

    click to expand
    Figure 11.3

  3. Using the mouse, drag the Visual Basic Editor command to the Database toolbar, and close the Customize dialog. The command will become a toolbar button, as shown in Figure 11.4.


    Figure 11.4

To add a References button to the Visual Basic window Standard toolbar, use the new Visual Basic Editor button to open that window, open the Customize dialog from the Standard toolbar, and drag the References command from the Tools category to that toolbar. You may also want to drag the Compile Project command from the Debug category to this toolbar; both buttons are shown in Figure 11.5.


Figure 11.5




Expert One-on-One(c) Microsoft Access Application Development
Expert One-on-One Microsoft Access Application Development
ISBN: 0764559044
EAN: 2147483647
Year: 2006
Pages: 124
Authors: Helen Feddema

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