You are currently offline, waiting for your internet to reconnect

ACC97: How to Run Macros in Other Office Programs

This article was previously published under Q177760
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.

You can use Automation to run a Microsoft Visual Basic for Applications Subprocedure that exists in a document of another Microsoft Office program.This article shows you how to run Sub procedures from four of the MicrosoftOffice programs that support Visual Basic (Microsoft Access, MicrosoftExcel, Microsoft PowerPoint, and Microsoft Word). Although some of theseprograms previously had their own macro languages (Excel 4.0 MacroLanguage, Access Basic, WordBasic), this article only discusses runningVisual Basic Sub procedures.

NOTE: A Sub procedure that does not accept any arguments is also known as amacro in Microsoft Excel, Microsoft PowerPoint, and Microsoft Word.Microsoft Access also has macros, but they are not equivalent to a VisualBasic 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 -

Microsoft Advisory Services -

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:;EN-US;CNTACTMSThe products listed at the beginning of this article all have very similarmeans of accessing a Sub procedure stored in their respective file formats.The following Sub procedures should work in any of the Microsoft Officeprograms. Any differences are noted.

To use any of these Sub procedures, you must place them in a module. Followthese steps to insert a module for the appropriate program.

Inserting a Module in Excel, PowerPoint or Word

  1. Press ALT+F11 to start the Visual Basic Editor.
  2. On the Insert menu, click Module.
  3. Type the appropriate sample code (found later in this section) in the module.

Inserting a Module in Microsoft Access

  1. In the database window, click the Modules tab.
  2. Click New.
  3. Type the appropriate sample code (found later in this section) in the module.

Sub Procedure to Run an Existing Microsoft Access Macro

The following Sub procedure assumes that the database AccessAutomation.mdbcontains a macro called "AccessMacro."
      Sub AccessTest1()      Dim A as Object      Set A = CreateObject("Access.Application")      A.Visible = False      A.OpenCurrentDatabase("C:\My Documents\AccessAutomation.mdb")      A.DoCmd.RunMacro "AccessMacro"   End Sub				
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 ofMicrosoft Access, it is always created as visible. All of the otherMicrosoft Office programs are created with the Visible property set toFalse by default.

Also, note that if a Microsoft Access Sub procedure displays a modal dialogbox, such as a message box, Microsoft Access has to be activated manuallyusing the Microsoft Windows 95 taskbar to view the dialog box. If the codedoes not display a modal dialog box, manual activation is not necessary.There is no way to do this activation programmatically with Automation. Allof the other programs listed at the beginning of this article displaydialog boxes without being activated.

Sub Procedure to Run an Existing Microsoft Excel Macro

The following Sub procedure assumes that the workbook ExcelFile.xlscontains a macro called "TestMacro."
     Sub XLTest()      Dim XL as Object      Set XL = CreateObject("Excel.Application")      XL.Workbooks.Open "C:\My Documents\ExcelFile.xls"      ' If there is more than one macro called TestMacro,      ' the module name would be required as in      '      ' XL.Run "Module1.TestMacro"      '      ' to differentiate which routine is being called.      '      XL.Run "TestMacro"   End Sub				

Sub Procedure to Run an Existing Microsoft PowerPoint Macro

The following Sub procedure assumes that the presentation PPTAutomation.pptcontains a macro called "AutomationTest."
     Sub PPTTest()      Dim PPT as Object      Set PPT = CreateObject("PowerPoint.Application")      PPT.Presentations.Open "C:\My Documents\PPTAutomation.ppt", , ,False      ' Note that the file name and the module      ' name are required to path the macro correctly.      PPT.Run "PPTAutomation.ppt!Module1.AutomationTest"   End Sub				

Sub Procedure to Run an Existing Microsoft Word Macro

The following Sub procedure assumes that the document WordDoc.Doc containsa macro called "WordMacro."
      Sub WDTest()      Dim WD as Object      Set WD = CreateObject("Word.Application")      WD.Documents.Open "C:\My Documents\WordDoc.Doc"      ' Note that the project name and module name are required to      ' path the macro correctly.      WD.Run "Project.Module1.WordMacro"   End Sub				
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
165518 Calling Macros Using OLE from MS Visual Basic for Applications
153307 HOWTO: Call Microsoft Excel Macros that Take Parameters
128405 XL: How to Run a WordBasic Macro from an MS Excel Macro
For additional information about getting help with Visual Basic for Applications, click the article number below to view the article in the Microsoft Knowledge Base:
163435 VBA: Programming Resources for Visual Basic for Applications
vba inf

Article ID: 177760 - Last Review: 12/05/2015 08:13:05 - Revision: 4.4

Microsoft Access 97 Standard Edition, Microsoft Visual Basic for Applications 5.0, Microsoft Excel 97 Standard Edition, Microsoft Word 97 Standard Edition, Microsoft PowerPoint 97 Standard Edition

  • kbnosurvey kbarchive kbcode kbhowto kbmacro kbprogramming KB177760