Microsoft KB Archive/213430

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

Article ID: 213430

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



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:

https://partner.microsoft.com/global/30000104

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
 Start Microsoft Word 2000, and then press ALT+11 to start the Visual Basic Editor. On the Insert menu, click Module.  Type the following macro in the new module sheet: Sub macro1 MsgBox "Word macro" End Sub  Press ALT+F11 to return to Word 2000. On the File menu, click Save As, and then save the document as C:\Wordtest.doc. Start Excel 2000, and then press ALT+F11 to start the Visual Basic Editor.</li> On Insert menu, click Module.</li>  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 </li> Press ALT+F11 to return to Excel.</li> Press ALT+F8 to open the Macro dialog box.</li> Click RunWordMacro_DDE in the Macro name list, and then click Run.</li></ol>

The macro starts Word 2000, and the "Word macro" message appears.

Automation Method
<ol> Follow steps 1 - 5 of the DDE method.</li> In Excel, press ALT+F11 to open the Visual Basic Editor.</li> On Insert menu, click Module.</li>  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 </li> On the Tools menu, click References.</li> Click Microsoft Word 9.0 Object Library in the Available References dialog box, and then click OK.</li> Press ALT+F11 to return to Excel.</li> Press ALT+F8 to open the Macro dialog box. Select RunWordMacro_Automation from the Macro name list, and then click Run.</li></ol>

The macro starts Word 2000, and the "Word macro" message appears.

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

<div class="references_section">