VB VBA in a Nutshell by Paul Lomax

VB VBA in a Nutshell by Paul Lomax

Author:Paul Lomax [Lomax, Paul]
Language: fra
Format: epub
Tags: Informatique
Publisher: First
Published: 0101-01-01T00:00:00+00:00


Getting a VB Program to Run

A global template file loaded during Word startup is displayed in the Project window visible in the development environment, but isn’t viewable. To edit the file, you must open it in the Word environment. Note that you may have to close and reopen Word in order for modifications to take effect. In some cases, even if the file is open, you still may not be able to edit its code in the VBA IDE. In

1fcProgram

Structure

that case, you’ll have to make modifications to a copy of the file stored in another directory and synchronize the two copies.

If your application applies to a set of documents that are based on a template (which is typically stored in the Office Template directory or one of its subdirectories), you can place your code in the template file. Each document created using that template maintains a reference to the template. So even though the code remains in the template and isn’t copied to the document, the VBA code in the template can be executed as long as the reference is valid.

If your application applies only to a particular document, you can store the code in the document itself. You don’t have to work with the templates loaded into Word’s global layer.

At startup

If you are developing an application or routine that is responsible for initializing the Word environment, that provides some service expected to be available throughout a Word session, or that implements a customized interface that mediates between the user and Word, you want to have Word launch your application whenever Word itself is launched. Word provides two methods of doing this. Both are remnants of WordBasic and both require that you store your macro in a global template:

Add a procedure called AutoExec to any code module. In order to execute at startup, it must be a Public procedure.

Create a new module named AutoExec and add a procedure to it called Main.

Once again, Main must be declared as Public in order to run at startup.

There is also a converse scenario—running a procedure when Word is closing—

that operates in exactly the same way as AutoExec. You simply name the procedure AutoExit or include an AutoExit module with a Main procedure.

When a document loads

In many cases, your application should launch whenever a particular document (or a set of documents, or even all documents) is opened. Once again, Word offers several methods of executing code when an existing document is opened or a new one is created. All of them require, though, that the code be located either in the current template or in the document itself. The methods are:

Creating a procedure called AutoOpen, which is executed whenever an existing document containing a reference to AutoOpen’s template or containing Getting a VB Program to Run 13

VB & VBA in a Nutshell: The Language, eMatter Edition Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.

Getting a VB Program to Run

the actual AutoOpen code is fired. Similarly, you can create a procedure called AutoNew, which is executed when a new document using the template containing the code is created.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.