Introducing the IDE

The IDE is essentially an Object Linking and Embedding (OLE) Automation interface for the Visual Basic Editor. After you establish a reference to the Visual Basic for Applications Extensibility Library (use the VBE's Tools ^ References command), you have access to all the VBE's objects, properties, and methods, and you can also declare objects from the IDE's member classes.

In the References dialog box, you can add a reference to Microsoft Visual Basic for Applications Extensibility. This gives you access to an object called VBIDE. Creating a reference to VBIDE enables you to declare object variables contained in the VBIDE and also gives you access to a number of predefined constants that relate to the IDE. Actually, you can access the objects in the IDE without creating a reference, but you won't be able to use the constants in your code nor will you be able to declare specific objects that refer to IDE components.

Refer to Chapter 20 for background information about OLE Automation.

After you understand how the IDE object model works, you can write code to perform a variety of operations, including the following:

♦ Adding and removing VBA modules

♦ Inserting VBA code

♦ Creating UserForms

♦ Adding controls to a UserForm

Important Note for Excel 2002 and 2003 Users

If you're using Excel to develop applications for others to use, be aware that things have changed, beginning with Excel 2002. To reduce the possibility of macro viruses, Microsoft made it much more difficult for a VBA macro to modify components in a VBA project. If you attempt to execute any of the procedures in this chapter, you will probably see an error message like the one that follows:

Whether you see this error message depends on a setting in Excel's Security dialog box (accessed by choosing Tools ^ Macro ^ Security). This setting, called Trust Access to Visual Basic Project, is turned off by default. Even if the user chooses to trust the macros contained in the workbook, the macros cannot modify the VBA project if this setting is turned off. Note that this setting applies to all workbooks and cannot be changed only for a particular workbook.

You can't access the value of this particular setting directly. The only way to detect this setting is to attempt to access the VBProject object and then check for an error. The following code demonstrates:

On Error Resume Next

Set x = ActiveWorkbook.VBProject

MsgBox "Your security settings do not allow this macro to run." Exit Sub

End If

Not all the examples in this chapter are intended to be used by end users. Many of them are designed to assist developers create projects. For these projects, you'll want to turn off the Trust Access to Visual Basic Project setting.

0 0

Post a comment