XL2000: How to Run a WordBasic Macro from a Microsoft Excel Macro (213430)



The information in this article applies to:

  • Microsoft Excel 2000

This article was previously published under Q213430

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.

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

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.

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


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbProgramming KB213430