How To Pass Arguments to a Word Macro by Using Dynamic Data Exchange (DDE) (274284)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows

This article was previously published under Q274284

SUMMARY

This article demonstrates how to use Dynamic Data Exchange (DDE) to call a Microsoft Word macro that requires one or more arguments.

Note When possible, Microsoft recommends that you use Automation, not DDE, to communicate with Microsoft Word. Microsoft Office applications support rich object models that provide functionality through Automation that is not available with DDE. Microsoft Word versions 7.0 and later support Automation.

MORE INFORMATION

To call a Word macro from DDE, you would typically use the ToolsMacro WordBasic command. However, to call a Word macro that accepts arguments, you cannot use ToolsMacro. Instead, you can call the macro by using a syntax such as ModuleName.MacroName(Arg1Value, Arg2Value).

The following steps demonstrate how to call a Word macro that accepts one or more arguments. In this example, a Microsoft Excel macro calls Word macros by using DDE.
  1. Start Microsoft Word.
  2. On the Tools menu, click Macros, and then click Visual Basic Editor.
  3. On the Insert menu, click Module to add a new module sheet into the VBA project for Normal.dot.
  4. On the View menu, click Properties Window, and then change the Name property of the module to DDETesting.
  5. Copy and paste the following macros into the DDETesting module:
    Sub Test (iNum As Integer)
        MsgBox "The value is: " & iNum
    End Sub
    
    Sub Test2 (strFname As String, strLname As String, iNum As Integer)
        MsgBox "The values are: " & strFname & "," & strLname & "," & iNum
    End Sub
    					
  6. Start a new workbook in Microsoft Excel.
  7. On the Tools menu, click Macros, and then click Visual Basic Editor.
  8. On the Insert menu, choose Module to add a new module sheet into the VBA project for the workbook.
  9. Copy and paste the following macro into the new module:
    Sub DDEtoWord()
        Dim chanNum As Variant
        chanNum = DDEInitiate("WinWord", "System")
        DDEExecute chanNum, "[DDETesting.Test(5)]"
        DDEExecute chanNum, "[DDETesting.Test2(""John"",""Doe"", 12)]"
        DDETerminate chanNum
    End Sub
    					
  10. On the Tools menu, click Macros. Select DDEtoWord in the list of macros, and then click Run. When the two Word macros run by using DDE, the macros display the value(s) of the parameter(s) that are passed by Microsoft Excel.

REFERENCES

For additional information about running a Word macro without passing arguments, click the following article number to view the article in the Microsoft Knowledge Base:

213430 How to run a WordBasic macro from a Microsoft Excel macro

For additional information about passing arguments to a Word macro by using automation, click the following article number to view the article in the Microsoft Knowledge Base:

172483 How To Run a Word 97 macro that requires arguments

For additional information about Office automation, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kbDDE kbhowto KB274284 kbAudDeveloper