VBA: How to Run Macros in Other Office Programs

ID: Q177760

The information in this article applies to:

SUMMARY

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.

MORE INFORMATION

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

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

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.mdb contains 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.Run "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 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 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.ppt contains 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 contains a 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

REFERENCES

For additional information, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q165518
   TITLE     : Calling Macros Using OLE from MS Visual Basic for
               Applicatons

   ARTICLE-ID: Q153307
   TITLE     : HOWTO: Call Microsoft Excel Macros that Take Parameters

   ARTICLE-ID: Q128405
   TITLE     : XL: How to Run a WordBasic Macro from an MS Excel Macro

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: vba
Keywords          : kbcode kbmacro kbprg 
Version           : WINDOWS:5.0,97
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 5, 1999