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/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor 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;CNTACTMSDynamic 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.
- On Insert menu, click Module.
- 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
- Press ALT+F11 to return to Excel.
- Press ALT+F8 to open the Macro dialog box.
- 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
- Follow steps 1 - 5 of the DDE method.
- In Excel, press ALT+F11 to open the Visual Basic Editor.
- On Insert menu, click Module.
- 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
- On the Tools menu, click References.
- Click Microsoft Word 9.0 Object Library in the Available References dialog box, and then click OK.
- Press ALT+F11 to return to Excel.
- 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.