Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Run a WordBasic Macro from a Microsoft Excel Macro


View products that this article applies to.

Summary

In Microsoft Excel 2000, you can programmatically create a Visual Basic for Applications macro that uses either Dynamic Data Exchange (DDE) or Automation with Microsoft Word 2000 to run a WordBasic macro. This article demonstrates these methods for running a WordBasic macro from Excel.

↑ Back to the top


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

Dynamic Data Exchange Method

  1. Start Microsoft Word 2000, and then press ALT+11 to start the Visual Basic Editor.
  2. On the Insert menu, click Module.
  3. Type the following macro in the new module sheet:
    Sub macro1()
       MsgBox "Word macro"
    End Sub
    					
  4. Press ALT+F11 to return to Word 2000.
  5. On the File menu, click Save As, and then save the document as C:\Wordtest.doc.
  6. Start Excel 2000, and then press ALT+F11 to start the Visual Basic Editor.
  7. On Insert menu, click Module.
  8. Type the following macro in the new module sheet:
    Sub RunWordMacro_DDE()
    Dim chan
    
       ' Initiate a channel with Microsoft Word.
       chan = DDEInitiate("Winword", "System")
    
       ' Execute a command on the channel to run the Wordbasic macro.
       DDEExecute chan, "[Toolsmacro.name=""Macro1"",.Run]"
    
       ' Terminate the channel.
       DDETerminate chan
    
    End Sub
    					
  9. Press ALT+F11 to return to Excel.
  10. Press ALT+F8 to open the Macro dialog box.
  11. Click RunWordMacro_DDE in the Macro name list, and then click Run.
The macro starts Word 2000, and the "Word macro" message appears.

Automation Method

  1. Follow steps 1 - 5 of the DDE method.
  2. In Excel, press ALT+F11 to open the Visual Basic Editor.
  3. On Insert menu, click Module.
  4. Type the following macro in the new module sheet:
    Sub RunWordMacro_Automation()
      Dim WordApp As Word.Application
      Dim WordDoc As Word.Document
      Set WordApp = CreateObject("Word.Application")
      Set WordDoc = WordApp.Documents.Open _
          ("C:\Wordtest.doc")
      WordApp.Visible = True
      WordApp.Run "macro1"
    
      ' Uncomment the next line of code to print the document.
      ' WordDoc.PrintOut Background:=False
    
      ' Uncomment the next line of code to save the modified document.
      ' WordDoc.Save
    
      WordApp.Quit SaveChanges:=wdDoNotSaveChanges
      Set WordApp = Nothing
    End Sub
    					
  5. On the Tools menu, click References.
  6. Click Microsoft Word 9.0 Object Library in the Available References dialog box, and then click OK.
  7. Press ALT+F11 to return to Excel.
  8. Press ALT+F8 to open the Macro dialog box. Select RunWordMacro_Automation from the Macro name list, and then click Run.
The macro starts Word 2000, and the "Word macro" message appears.

NOTE: You must save the Word 2000 document as C:\Wordtest.doc.

↑ Back to the top


References

For additional information about Automation, click the article number%2 below to view the article%2 in the Microsoft Knowledge Base:
212890 How To Run a Microsoft Word 2000 Macro Using Automation

↑ Back to the top


Keywords: KB213430, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 213430
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 425