外文翻译--VBA的开发环境 下载本文

内容发布更新时间 : 2024/5/19 1:49:31星期一 下面是文章的全部内容请认真阅读。

8. Enter code into the procedure.

For more information about procedures, see the Microsoft Visual Basic online help reference. Adding user forms

If you want your code to prompt the user for information, or you want to display the result of some action performed when the user invokes an ArcGIS application command or tool,or in response to some other event, use VBA's user forms. User forms provide a context in which you can provide access to a rich set of integrated controls. Some of these controls are similar to the UIControls that are available as part of the Customize dialog box's Commands tab. In addition to text boxes or command buttons, you have access to a rich set of additional controls. A user form is a container for user-interface controls, such as command buttons and text boxes. A control is a Visual Basic object you place on a user form that has its own properties, methods, and events. You use controls to receive user input, display output, and trigger event procedures. You can set the form to be either modal, in which case the user must respond before using any other part of the application, or modeless, in which case subsequent code is executed as it's encountered.

To add and start coding in a user form

1. In the Project Explorer, select the Project to which you want to add a user form. 2. Click the Insert menu and click UserForm.

3. VBA inserts a user form into your project and opens the Controls Toolbox.

4. Click the controls that you want to add to the user in from the Controls Toolbox. 5. Add code to the user form or to its controls.

For more information about adding controls, see the Microsoft Visual Basic online help reference.

To display the Code window for a user form or control, double-click the user form or control. Then, choose the event you want your code to trigger from the dropdown list of events and procedures in the Code window and start typing your code. Or, just as in a module or class module, insert a procedure and start typing your code.

To display the form during an ArcMap or ArcCatalog session in response to some action, invoke its Show method, as in this example: UserForm1.Show vbModeless 'show modeless Some VBA project-management techniques

To work efficiently in the ArcGIS application's VBA development environment, and reduce the amount of work you have to do every time you start a new task, make use of several techniques that will streamline your work: Reusing modules, class modules, and user forms

To add an existing module or form to the Normal template, the Project, or a TemplateProject, click the name of the destination in the Project Explorer, then choose Import File from the File menu. You can choose any VBA module, user form, or class module to add a copy of the file to your project. To export an item from your project so that it is available for importing into other projects, select the item you want to export in the Project Explorer, choose Export File from the File menu, then navigate to where you want to save the file. Exporting an item does not remove it from your project. Removing project items

When you remove an item, it is permanently deleted from the project list梱ou can't undo the Remove action; however, this action doesn't delete a file if it exists on disk. Before removing an item, make sure remaining code in other modules and user forms doesn't refer to code in the removed item. To remove an item, select it in the Project Explorer, then choose Remove from the File menu. Before you remove the item, you'll be asked whether you want to export it. If you click Yes in the message box, the Export File dialog box opens. If you click No, VBA deletes the item. Protecting your code

To protect your code from alteration and viewing by users, you can lock a Project, a TemplateProject, or even Normal. When you lock one of these items, you set a password that must be entered before it can be viewed in the Project Explorer. To lock one of these items, right-click Project, TemplateProject, or Normal in the Project Explorer, then click the Properties item in the context menu that appears. In the Properties dialog box, click the Protection tab and click the option to Lock Project for Viewing. Enter a password and confirm it. Finally, save your ArcMap or ArcCatalog file and close it. The next time you or anyone else opens the file, the project is locked. If anyone wants to view or edit the project, they must enter the password. Saving a VBA project

VBA projects are stored in a file that can be a base template (*.mxt), the Normal template, or a document (*.mxd). When a user creates a new ArcMap document from a base template, the new document references the base template's VBA project and its items. To save your ArcMap document and your VBA project, click Save from the ArcMap File menu or Save from the File menu in the Visual Basic Editor. Both commands save your file with the project and any items stored in it. After saving the file, its filename is displayed in the Project Explorer in parentheses after the project name. To save the document as a template, click Save As from the ArcMap File menu and specify ArcMap Templates (*.mxt) as the File type. Running VBA code

As you build and refine your code, you can run it within VBA to test and debug it. This section discusses running your code in the Visual Basic Editor during design time. For more

information about running and debugging a VBA program, such as adding breakpoints, adding watch expressions, and stepping into and out of execution, see Microsoft Visual Basic online help.

To run your code in the Visual Basic Editor or from the Macros dialog box 1. Click the Tools menu and click Macros.

2. In the Macro list, click the macro you want and click Run.

If the macro you want is not listed, make sure you've chosen the appropriate item: either Normal, Project, or TemplateProject in the Macros In box. Private procedures do not appear in any menus or dialog boxes.

To run only one procedure in the Visual Basic Editor

1. In the Project Explorer, open the module that contains the procedure that you want to run.

2. In the Code window, click an insertion point in the procedure code. 3. Click the Run menu and click Run Sub/UserForm. Only the procedure in which your cursor is located runs. After you've finished writing your code

After you have finished writing code, users can run it from ArcMap or ArcCatalog. To do this, they choose Macros and then Macros from the Tools menu. You can also associate the code with a command or tool, or it can run in response to events or in other ways that you design. Using the Global Application objects

Application and ThisDocument are examples of global system variables that can be accessed by any module or class in the VBA environment while ArcMap is running. This variable is automatically set to reference the current document when ArcMap opens the document. You can use ThisDocument as a shortcut when programming in VBA to access the current document. Here is an example of how to use both the Application and ThisDocument: Dim pMxDoc as IMxDocument Set pMxDoc =Application.Document 'or

Set pMxDoc =ThisDocument