Creating a VBA Project

3 4

A Microsoft Visual Basic for Applications (VBA) project consists of modules, class modules, and user forms.

  • A module is a set of declarations followed by procedures—a list of instructions that a program performs.
  • A class module defines an object, its properties, and its methods. A class module acts as a template from which an instance of an object is created at run time. Every Visio-based VBA project contains a class module called ThisDocument ; this class module represents the properties, methods, and events of the Microsoft Visio document that contains the VBA project.
  • A user form contains user interface controls, such as command buttons and text boxes.

A simple project might require a single user form or module, although more complex projects might contain multiple modules, class modules, and user forms. For example, a solution that generates a drawing could include a user form to gather information from the user. It could also include code, organized in one or more modules and/or class modules, that does the following:

  • Validates the user's input
  • Translates the code into data that could be used to generate a drawing
  • Uses Automation to create the drawing in the Visio drawing window

If the solution should run in response to an event, such as opening a document, the solution could include code that would run when that event occurred.

You can add existing modules, class modules, or user forms to your project by importing files; or, you can export project items for use in other projects. You can also insert an ActiveX control into your Visio document. For details about ActiveX controls, see Chapter 24, Using ActiveX Controls in a Visio Solution.

Inserting Modules and Class Modules into Your Project

Many VBA programs contain one or more modules—a set of declarations followed by procedures. Every Visio VBA project contains the class module ThisDocument, which is an object that represents the project's document. You can create additional class modules to define custom VBA objects in your project.

To insert a module or class module

  • In the Visual Basic Editor, on the Insert menu, click Module or Class Module.
  • The Code window will display an empty module where you will insert procedures that create templates into which you enter VBA code.

Figure 15-4.  The <b>Add Procedure</b> dialog box <b>.</b>

Figure 15-4 The Add Procedure dialog box .

To add procedures to modules and class modules

  1. In the Visual Basic Editor, on the Insert menu, click Procedure to open the Add Procedure dialog box.
  2. In the Name box, name the procedure.
  3. The procedure's name is displayed on its module's submenu on the Visio Macros submenu on the Tools menu. A procedure name cannot include spaces or reserved words (such as MsgBox, If, or Loop) that VBA uses as part of its programming language.

  4. Under Type, choose the type of procedure: Sub, Function, or Property.
  5. Modules and class modules can contain more than one type of procedure.

    • To write a procedure that takes no arguments, insert a Sub procedure.
    • To write a function that takes arguments and returns a value, insert a Function procedure.
    • To add properties to a class module, insert a Property procedure.
  6. Under Scope, choose Public or Private.
  7. Scope is the extent to which a procedure can be accessed by other modules and programs.

    • A procedure with a private scope can be accessed by only the module that contains it; only a procedure within the same module can call a private procedure, and a private procedure does not appear on any menus or in any dialog boxes.
    • A procedure with a public scope can be accessed by other programs and modules. Visio displays public procedures of modules and the ThisDocument class module that take no arguments on the Macros submenu.
  8. To declare all local variables as static, select the All Local variables as Statics check box.
  9. You can declare the variables in your procedure as local or static (global).

    • Static variables exist for the lifetime of your entire program.
    • Local variables exist only while the procedure that they are declared in is running. The next time the procedure is executed, all local variables are reinitialized. However, you can preserve for the lifetime of your program the value of all local variables in a procedure by making them static, which fixes their value.
  10. Click OK.
  11. VBA inserts a procedure template into the item's Code window into which you can enter code. The template contains the first and last lines of code for the type of procedure you insert.

For details about procedures, see the Microsoft Visual Basic Help.

Inserting User Forms into Your Project

If you want your program to prompt the user for information, you can build a user interface by inserting user forms. A user form contains user interface controls, such as command buttons and text boxes. When you add a user form to your project, VBA automatically opens the Toolbox of controls. A control is an object you place on a user form that has its own properties and methods; a control also fires events that you can respond to. You use controls to receive user input, display output, and trigger event procedures.

Figure 15-5.  <b>Toolbox</b> and user form containing controls.(<b>A</b>) <b>Controls</b> tab in the <b>Toolbox.</b> (<b>B</b>) User form. (<b>C</b>) Text box control. (<b>D</b>) Command button control.

Figure 15-5 Toolbox and user form containing controls.(A) Controls tab in the Toolbox. (B) User form. (C) Text box control. (D) Command button control.

To add a user form to your project

  1. On the Insert menu, click UserForm.
  2. From the Toolbox, drag the controls that you want onto the user form.
  3. For details about adding controls, such as command buttons and text boxes, see the Microsoft Visual Basic Help.

  4. Double-click a user form or control to display its Code window.
  5. Click the event that you want to respond to in the drop-down list of events and procedures in the Code window; you can then start typing your code. Or, insert a procedure and start typing your code in the procedure template.

Tip


This guide uses the VBA default user form and control names for clarity, but it is considered good programming technique to change the default names to something more descriptive. Many programmers use the following naming conventions:
 User form default name = UserForm1 Revised name = frmGetDocName 

Notice the use of frm in the revised name of the user form. In the control name, many programmers use frm (form), txt (textbox), lbl (label), cmd (command button), and so on, so you can quickly recognize the type of object.

Importing Files into and Exporting Files from Your Project

To import an item into your project, on the File menu, click Import File. You can choose any VBA module (files with a .bas extension), user form (files with an .frm extension), or class module (files with a .cls extension) to add a copy of the file to your project. To export an item from your project so that it will be available for importing into other projects, select the item you want to export in the Project Explorer, then on the File menu, click Export File and enter the location in which you want to save the file. Exporting an item does not remove it from your project.

You can also drag projects or project items between Visio files by selecting the project or project item you want to move in the Project Explorer and then dragging its icon onto a Visio project icon. A project item is automatically stored in the correct project folder. A project is referenced in the References folder, because a Visio file can contain only one project, but that project can reference other projects.

Note


You cannot drag the ThisDocument class module between Visio files, but you can drag (or copy and paste) code from ThisDocument into other project items.



Developing Microsoft Visio Solutions 2001
Developing Microsoft Visio Solutions (Pro-Documentation)
ISBN: 0735613532
EAN: 2147483647
Year: 2004
Pages: 180

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