Microsoft KB Archive/274284

= How to pass arguments to a Word Macro by using Dynamic Data Exchange (DDE) =

Article ID: 274284

Article Last Modified on 4/12/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Word 97 Standard Edition

-



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.  Start Microsoft Word. Press ALT+F11 to start Visual Basic Editor. On the Insert menu, click Module to add a new module sheet into the VBA project for Normal.dot. On the View menu, click Properties Window, and then change the Name property of the module to DDETesting .  Copy and paste the following macros into the DDETesting module: Sub Test (iNum As Integer) MsgBox &quot;The value is: &quot; & iNum End Sub

Sub Test2 (strFname As String, strLname As String, iNum As Integer) MsgBox &quot;The values are: &quot; & strFname & &quot;,&quot; & strLname & &quot;,&quot; & iNum End Sub  Start a new workbook in Microsoft Excel. Press ALT+F11 to start Visual Basic Editor.</li> On the Insert menu, choose Module to add a new module sheet into the VBA project for the workbook.</li>  Copy and paste the following macro into the new module: Sub DDEtoWord Dim chanNum As Variant chanNum = DDEInitiate(&quot;WinWord&quot;, &quot;System&quot;) DDEExecute chanNum, &quot;[DDETesting.Test(5)]&quot; DDEExecute chanNum, &quot;[DDETesting.Test2(&quot;&quot;John&quot;&quot;,&quot;&quot;Doe&quot;&quot;, 12)]&quot; DDETerminate chanNum End Sub </li> 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.</li></ol>

<div class="references_section">