Microsoft KB Archive/94713

-

{| The information in this article applies to:
 * width="100%"|
 * Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, and 2.0c
 * Microsoft Excel for Windows, versions 3.0 and 4.0
 * Microsoft Windows operating system versions 3.0 and 3.1

SUMMARY
This article contains four macros that use dynamic data exchange (DDE) to interact between Microsoft Word for Windows and Microsoft Excel.

Macro 1: Running Excel from Word for Windows Macro 2: Running Word for Windows from Excel Macro 3: Running an Excel Macro from Word for Windows Macro 4: Running a Word for Windows macro from Excel

Line explanations follow each macro.

Note: The line numbers at the beginning of each line of the macros are for reference only and are not part of the macro. Your macro will not run and error messages will occur if you retain the line numbers.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Macro 1: Running Excel from Word for Windows
This Word macro uses DDE to start Excel, insert data in an Excel spreadsheet, calculate the information, format the result, and insert it into a Word document.


 * 1) Sub MAIN 2. channel = DDEInitiate(&quot;excel&quot;, &quot;sheet1&quot;) 3. DDEPoke channel, &quot;R1C1&quot;, &quot;12&quot; 4. DDEPoke channel, &quot;R2C1&quot;, &quot;10&quot; 5. DDEPoke channel, &quot;R3C1&quot;, &quot;=A1*A2&quot; 6. DDEExecute channel, &quot;[format.number(&quot; + Chr$(34) +\ 7. &quot;$#,##0.0&quot; + Chr$(34) + &quot;)]&quot; 8. a$ = DDERequest$(channel, &quot;R3C1&quot;) 9. DDETerminate channel

10. Insert a$ 11. End Sub Line descriptions:

2. From Word, open a DDE channel to Sheet1 in Excel. 3, 4. Insert values into spreadsheet cells. 5. Insert function into spreadsheet. Excel calculates cell value. 6. Format cell contents using the Format Number command in Excel. 7. Assign the calculated and formatted value to a string variable in Word for Windows. 8. Close the connection to Excel. 9. Insert the string into the Word document.

Macro 2: Running Word for Windows from Excel
This sample Excel macro uses DDE to start Word for Windows, open and initiate a channel to the AUTOEXEC.BAT file, insert some text and the creation date, save the file as AUTOEXEC.BAK, and exit Word for Windows.

A1. =SEND.KEYS(&quot;~&quot;, TRUE) A2. =EXEC(&quot;c:\winword\winword.exe c:\autoexec.bat&quot;, 3) A3. =INITIATE(&quot;winword&quot;, &quot;c:\autoexec.bat&quot;) A4. =EXECUTE(A3, &quot;[insertbookmark .name=&quot;&quot;test&quot;&quot;]&quot;) A5. =EXECUTE(A3, &quot;[insertfield .field = &quot;&quot;createdate \@ mm/dd/yy&quot;&quot;]&quot;) A6. Backup created: A7. =POKE(A3,&quot;test&quot;,A6) A8. =EXECUTE(A3, &quot;[insertpara]&quot;) A9. =EXECUTE(A3, &quot;[filesaveas .name=&quot;&quot;c:\autoexec.bak&quot;&quot;]&quot;) A10.=EXECUTE(A3, &quot;[fileexit]&quot;) A11.=TERMINATE(A3) A12.=RETURN

Line Descriptions:

A1. Send the ENTER keystroke to the Convert File dialog box in Word. A2. Start and maximize Word and open the AUTOEXEC.BAT file. A3. Initiate a channel to the AUTOEXEC.BAT file. A4. Insert a bookmark in the document. A5. Insert the file creation date at current location. A6. Text string. A7. Insert a text string at the bookmarked location. A8. Insert a paragraph mark at the current insertion point location. A9. Save a copy of the file as &quot;AUTOEXEC.BAK&quot;. A10. Exit Word for Windows. A11. Terminate channel to Word for Windows.

Macro 3: Running an Excel Macro from Word for Windows
This Word macro creates a DDE link from Word for Windows (the client) to Excel (the server), runs an Excel macro called MACRO1.XLM and then terminates the connection. Before you run this Word macro, create the following two-line macro in cells A1 and A2 in Excel:

A1: Beep A2: Return

Save this macro as MACRO1.XLM. Excel must be open and MACRO1.XLM must be the active sheet when you run Macro 3 (if you want the macro to determine whether Excel is running, use the GetModuleHandle command from the Windows KERNEL library. For more information on GetModuleHandle, refer to the Microsoft Windows Software Development Kit (SDK) or page 108 of &quot;Using WordBasic&quot;).


 * 1) Sub MAIN

2. channel = DDEInitiate(&quot;Excel&quot;, &quot;c:\excel\macro1.xlm&quot;) 3. DDEExecute channel, &quot;[run(&quot; + Chr$(34) + &quot;Macro1.xlm!R1C1&quot; + \  Chr$(34) + &quot;)]&quot; 4. DDETerminate channel 5. End Sub Line Descriptions:


 * 1) Initiate a DDE channel with Excel.

3. Send the Excel Run command to Excel to execute the macro. 4. Terminate the channel to Excel.

Macro 4: Running a Word for Windows Macro from Excel
Before you run this Excel macro, create the following three-line global macro named &quot;test&quot; in Word for Windows:

Sub MAIN MsgBox &quot;DDE from Excel successful!&quot; End Sub This Excel macro creates a DDE link from Excel (the client) to Word for Windows (the server), runs a Word for Windows Macro named &quot;test&quot;, and then terminates the connection. A1. =REGISTER(&quot;KERNEL&quot;,&quot;GetModuleHandle&quot;,&quot;IC&quot;) A2. =CALL(A1,&quot;MSWORD&quot;) A3. =IF(A2 = 0) A4. =EXEC(&quot;winword.exe&quot;) A5. =END.IF A6. =INITIATE(&quot;winword&quot;, &quot;system&quot;) A7. =EXECUTE(A6,&quot;[toolsmacro .name=&quot;&quot;test&quot;&quot;, .run]&quot;) A8. =TERMINATE(A6) A9. =RETURN

Line descriptions:

A1-A2. Call the GetModuleHandle function to determine if Word for

Windows is running. A3-A5. If the call to GetModuleHandle returns a zero then start Word for Windows. A6. Initiate a DDE channel with Word for Windows. A7. Run the &quot;test&quot; macro. A8. Terminate the DDE channel to Word for Windows. Reference(s):

&quot;Using WordBasic,&quot; by WexTech Systems and Microsoft, pages 102-115, 171-172
 * }

-

Last reviewed: July 30, 1997

© 1998 Microsoft Corporation. All rights reserved. Terms of Use.