ACC97: How to Run Macros in Other Office Programs

Article translations Article translations
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.

Expand all | Collapse all

On This Page

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 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 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.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 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, 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

Properties

Article ID: 177760 - Last Review: January 22, 2007 - Revision: 4.4
APPLIES TO
  • 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
Keywords: 
kbcode kbhowto kbmacro kbprogramming KB177760
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.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com