Article ID: 177760 - View products that this article applies to.
This article was previously published under Q177760
Advanced: Requires expert coding, interoperability, and multiuser skills.
You can use Automation to run a Microsoft Visual Basic for Applications Sub procedure that exists in a document of another Microsoft Office program. This article shows you how to run Sub procedures from four of the Microsoft Office programs that support Visual Basic (Microsoft Access, Microsoft Excel, Microsoft PowerPoint, and Microsoft Word). Although some of these programs previously had their own macro languages (Excel 4.0 Macro Language, Access Basic, WordBasic), this article only discusses running Visual Basic Sub procedures.
NOTE: A Sub procedure that does not accept any arguments is also known as a macro in Microsoft Excel, Microsoft PowerPoint, and Microsoft Word. Microsoft Access also has macros, but they are not equivalent to a Visual Basic Sub procedure.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners - https://partner.microsoft.com/global/30000104
Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
(http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms)The products listed at the beginning of this article all have very similar means of accessing a Sub procedure stored in their respective file formats. The following Sub procedures should work in any of the Microsoft Office programs. Any differences are noted.
To use any of these Sub procedures, you must place them in a module. Follow these steps to insert a module for the appropriate program.
Inserting a Module in Excel, PowerPoint or Word
Inserting a Module in Microsoft Access
Sub Procedure to Run an Existing Microsoft Access MacroThe following Sub procedure assumes that the database AccessAutomation.mdb contains a macro called "AccessMacro."
There are a couple of things to note when calling a Microsoft Access macro. First, when you use the CreateObject function to create an instance of Microsoft Access, it is always created as visible. All of the other Microsoft Office programs are created with the Visible property set to False by default.
Also, note that if a Microsoft Access Sub procedure displays a modal dialog box, such as a message box, Microsoft Access has to be activated manually using the Microsoft Windows 95 taskbar to view the dialog box. If the code does not display a modal dialog box, manual activation is not necessary. There is no way to do this activation programmatically with Automation. All of the other programs listed at the beginning of this article display dialog boxes without being activated.
Sub Procedure to Run an Existing Microsoft Excel Macro
The following Sub procedure assumes that the workbook ExcelFile.xls contains a macro called "TestMacro."
Sub Procedure to Run an Existing Microsoft PowerPoint MacroThe following Sub procedure assumes that the presentation PPTAutomation.ppt contains a macro called "AutomationTest."
Sub Procedure to Run an Existing Microsoft Word MacroThe following Sub procedure assumes that the document WordDoc.Doc contains a macro called "WordMacro."
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/165518/EN-US/ )Calling Macros Using OLE from MS Visual Basic for Applications
(http://support.microsoft.com/kb/153307/EN-US/ )HOWTO: Call Microsoft Excel Macros that Take Parameters
128405For additional information about getting help with Visual Basic for Applications, click the article number below to view the article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/128405/EN-US/ )XL: How to Run a WordBasic Macro from an MS Excel Macro
(http://support.microsoft.com/kb/163435/EN-US/ )VBA: Programming Resources for Visual Basic for Applications
Article ID: 177760 - Last Review: January 22, 2007 - Revision: 4.4
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.