Microsoft KB Archive/68510

= Opening and Closing Word for Windows Using an Excel Macro =

Article ID: 68510

Article Last Modified on 11/16/2006

-

APPLIES TO


 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0c
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Excel 4.0 Standard Edition
 * Microsoft Excel 3.0 Standard Edition
 * Microsoft Excel 2.10d
 * Microsoft Excel 2.1 Standard Edition
 * Microsoft Excel 2.01
 * Microsoft Excel 2.0 Standard Edition

-



This article was previously published under Q68510



In Microsoft Excel, you can write a macro that will open and close Microsoft Word for Windows from Microsoft Excel. To create this macro, do the following:


 * 1) Write a macro that will create an open channel number.
 * 2) Run Word for Windows.
 * 3) Close Word for Windows.
 * 4) Close the open channel number.

Microsoft provides macro 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 macro is provided 'as is' and Microsoft does not guarantee that the following code can be used in all situations. Microsoft does not support modifications of the code to suit customer requirements.

Sample Macro 1
The following sample Microsoft Excel macro, starts Word for Windows, initiates a DDE channel using the topic &quot;System,&quot; and terminates the open channel number:

A1: =EXEC(&quot;winword&quot;)

A2: =INITIATE(&quot;winword&quot;,&quot;system&quot;)

A3: =EXECUTE(A2,&quot;[FileExit(1)]&quot;)

A4: =TERMINATE(A2)

A5: =RETURN

NOTE: This macro assumes that Word for Windows is in your path.

Sample Macro 2
The following Microsoft Excel macro, initiates a DDE Channel to Microsoft Word for Windows. In addition, it checks to see if Word for Windows is currently running. If it is, the macro initiates the channel to the current session of Word. This eliminates the problem of multiple instances of Word for Windows being opened unnecessarily. If Word for Windows is not running the macro starts Word.

A1: =SET.NAME(&quot;HANDLE&quot;,REGISTER(&quot;kernel&quot;,&quot;getModuleHandle&quot;,&quot;IC&quot;))

A2: =SET.NAME(&quot;CALLVALUE&quot;,CALL(Handle,&quot;WINWORD.EXE&quot;))

A3: =IF(CallValue=0)

A4: =EXEC(&quot;WINWORD.EXE&quot;)

A5: =END.IF

A6: =INITIATE(&quot;WINWORD&quot;,&quot;SYSTEM&quot;)

A7: =RETURN

Note: this is also true for Microsoft Excel version 5.0 macro sheets when you use version 4.0 macros.

