XLM: Opening and Closing Word for Windows Using an Excel Macro

ID: Q68510




The information in this article applies to:

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 "System," and terminates the open channel number:

A1: =EXEC("winword")
A2: =INITIATE("winword","system")
A3: =EXECUTE(A2,"[FileExit(1)]")
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("HANDLE",REGISTER("kernel","getModuleHandle","IC"))
A2: =SET.NAME("CALLVALUE",CALL(Handle,"WINWORD.EXE"))
A3: =IF(CallValue=0)
A4: =EXEC("WINWORD.EXE")
A5: =END.IF()
A6: =INITIATE("WINWORD","SYSTEM")
A7: =RETURN()

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

REFERENCES

"Function Reference," version 4.0, pages 240-241
"Function Reference," version 3.0 for Windows, pages 130-131
"Functions and Macros," version 2.0 for Windows, page 307
"Microsoft Word for Windows Technical Reference," page 49

Additional query words: 2.0 2.00 2.01 2.1 2.10


Keywords          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: March 21, 1999