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 object, you have access to all the VBE's objects, properties, and methods , and you can also declare objects from the IDE's member classes.

Use the VBE's Tools image from book References command to display the References dialog box, where you can add a reference to the Microsoft Visual Basic for Applications Extensibility Library (see Figure 28-1). 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 .

image from book
Figure 28-1: Adding a reference to the Microsoft Visual Basic for Applications Extensibility Library.
CROSS-REFERENCE  

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

image from book
An Important Security Note

If you're using Excel to develop applications for others to use, be aware that the procedures in this chapter may not work. Because of the threat of macro viruses, Microsoft (beginning with Excel 2002) 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 may see an error message.

Whether you see this error message depends on a setting in Excel's Trust Center dialog box. To view or change this setting,

  1. Choose Office image from book Excel Options.

  2. In the Excel options dialog box, click the Trust Center Tab.

  3. In the Trust Center Tab, click the Trust Center Settings button.

  4. In the Trust Center dialog box, click the Macro Settings tab.

You'll find a check box labeled Trust Access to the VBA Project Object Model.

This setting 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 for only a particular workbook.

You can't directly determine the value of this particular setting by using VBA. 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  = ActiveWorkbook.VBProject If Err <> 0 Then   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 help developers create projects. For these projects, you'll want to enable the Trust Access to Visual Basic Project setting.

image from book
 



Excel 2007 Power Programming with VBA
Excel 2007 Power Programming with VBA (Mr. Spreadsheets Bookshelf)
ISBN: 0470044012
EAN: 2147483647
Year: 2007
Pages: 319

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