Calling Word from Another Application

It is not possible to call a WLL from an application other than Word and then have the called WLL drive Word. The CAPI messaging interface (CMI) is a way to get around this problem.

Windows 3.x

To call wdCommandDispatch, a WLL must be called by Word; otherwise stack problems occur, resulting in a general protection fault. When making a Word API call, the calling application can send a message to Word (WM_WDCAPI) instead of calling wdCommandDispatch. As a result, Word calls wdCommandDispatch from its own stack (avoiding stack problems) when it processes the message.

To use the CAPI messaging interface, follow this general procedure.

1. Include the following in a header file:


#include "wdcapi.h"
#define WM_WDCAPI    (WM_USER + 0x0300)
typedef struct
{
    short    CommandID;
    short    DlgOptions;
    short    cArgs;
    LPWDOPR    lpwdoprArgs;
    LPWDOPR    lpwdoprReturn;
} CMI;
typedef CMI far *LPCMI;

2. Fill the CMI structure.

3. Call SendMessage in the following way:


err = (int)SendMessage( hWordWnd, WM_WDCAPI, 0, (LPARAM)(LPCMI)&cmi );

Windows 95 and Windows NT

To access the CMI from Windows 95 or Windows NT, you call cmiCommandDispatch instead of sending the WM_WORDCAPI message to Word (in Windows 95 and Windows NT, WM_WORDCAPI returns CAPIBadMessage). To call cmiCommandDispatch, you can either link your application with the WIN32CMI.LIB file, or load the WIN32CMI.DLL with LoadLibrary and then call GetProcAddress to get the address of the function.
The cmiCommandDispatch function has the same prototype as wdCommandDispatch; see the CMI header file (WIN32CMI.H) for this prototype.

The WIN32CMI.LIB and WIN32CMI.DLL files are located in the CAPI\WIN32CMI folder on the Microsoft Word Developer's Kit disk. The folder includes the source files required to build the LIB and DLL (a Microsoft Visual C++ version 2.0 project file is provided). If you are programming on a RISC platform, you will need to build the LIB and DLL for your platform using the source files provided.

Macintosh

On the Macintosh, you can use the CMI to drive Word from an application or code resource that is not directly called by Word. CMI on the Macintosh uses Apple Events to dispatch commands to Word instead of calling wdCommandDispatch directly from the Word API.

To make a CMI call on the Macintosh, you calling application or code resource needs to build an Apple Event that contains all the information normally passed by wdCommandDispatch. After it sends the event to Word, it needs to unpack the reply event.

To use the CMI on the Macintosh, use the CMILIB.C and CMILIB.H files included in the CMI folder in the CAPI folder on the Microsoft Word Developer's Kit disk instead of the CAPILIB.C and CAPILIB.H described earlier in this appendix. With these files added to your project, you can simply make calls to cmiCommandDispatch instead of wdCommandDispatch; all of the Apple Event construction is handled by the CMI files. Review the sample provided in the CMI:SAMPLE foler to see how the CMI is used.