Combining VBA and Document-Level Customizations
Applies to |
---|
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office. Project type
Microsoft Office version
For more information, see Features Available by Application and Project Type. |
You can use Visual Basic for Applications (VBA) code in a document that is part of a Visual Studio Tools for Office document-level customization for Microsoft Office Word or Microsoft Office Excel. In all document-level projects, you can call VBA code in the document from the customization assembly. In document-level projects for the 2007 Microsoft Office system, you can also configure your project to enable VBA code in the document to call code in the customization assembly. For more information, see Calling Code in Document-Level Customizations from VBA.
Behavior of VBA Code in a Document-Level Customization
When you open your project in Visual Studio, the document is opened in design mode. VBA code does not run when the document is in design mode, so you can work on the document and code without running the VBA code.
When you run the solution, event handlers in both VBA and the Visual Studio Tools for Office assembly pick up events that are raised in the document, and both sets of code run. You cannot determine beforehand which code will run before the other; you must determine this through testing in each individual case. You can get unexpected results if the two sets of code are not carefully coordinated and tested.
Calling VBA Code from the Customization Assembly
You can call macros in Word documents, and you can call macros and functions in Excel workbooks. To do this, use one of the following methods:
For Word, call the Run(String, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object) method of the Application class.
For Excel, call the Run(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object) method of the Application class.
For each method, the first parameter identifies the name of the macro or function you want to call, and the remaining optional parameters specify the parameters to pass to the macro or function. The first parameter can have different formats for Word and Excel:
For Word, the first parameter is a string that can be any combination of template, module, and macro name. If you specify the document name, your code can only run macros in documents related to the current context — not just any macro in any document.
For Excel, the first parameter can be a string that specifies the macro name, a Range that indicates where the function is, or a register ID for a registered DLL (XLL) function. If you pass a string, the string will be evaluated in the context of the active sheet.
The following code example shows how to call a macro named MyMacro from a document-level project for Excel. This example assumes that MyMacro is defined in Sheet1.
Globals.Sheet1.Application.Run("MyMacro")
Globals.Sheet1.Application.Run("MyMacro", missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing);
Note
For information about using the global missing variable in place of optional parameters in Visual C#, see Programming with Visual Basic vs. C# in Office Solutions.
See Also
Tasks
How to: Expose Code to VBA in a Visual Basic Project
How to: Expose Code to VBA in a Visual C# Project
Walkthrough: Calling Code from VBA in a Visual Basic Project
Walkthrough: Calling Code from VBA in a Visual C# Project
Concepts
Calling Code in Document-Level Customizations from VBA
Creating Office Solutions in Visual Studio