Strange crash automating Word 2000 from MFC wrapper

D

D-Code-99

Hi guys,

I have a strange automation problem. I want to automate an existing instance
of MS Word 2000 from my MFC code. I've got the system working but I've run
into a strange bug.

My test to confirm it's working is to get Word to close the current
document. This is easy enough. I use this code:

void MyFunc()
{
_MSWord2000Application objWord;
_Document objDoc;

// Convenient values declared as ColeVariants.
COleVariant covTrue((short)TRUE), covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

::CoInitialize(NULL);

// Translate server ProgID into a CLSID. ClsidFromProgID
// gets this information from the registry.
CLSID clsid;
CLSIDFromProgID(L"Word.Application", &clsid);

// Get an interface to the running instance, if any..
IUnknown *pUnk;
HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
ASSERT(!FAILED(hr));

// Get IDispatch interface for Automation...
IDispatch *pDisp;
hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pDisp);
ASSERT(!FAILED(hr));

// Release the no-longer-needed IUnknown...
pUnk->Release();

// Attach the IDispatch interface to the class wrapper
objWord.AttachDispatch(pDisp);

// Test control: this opens a new window of the existing document
//objWord.NewWindow();

// Attach a document object to the current active document
objDoc.AttachDispatch(objWord.GetActiveDocument());

objDoc.Close(covOptional, covOptional, covOptional);

objDoc.ReleaseDispatch();
objWord.ReleaseDispatch();
}

The code is basically taken from MSDN and a little playing around to attach
to an existing instance of Word rather than create a new one.

The code itself works. When it's called, the active instance of Word
immediately closes the current document if it's saved. This is fine.

The problem is with an unsaved document. The code runs, and Word prompts
whether to save the document, forget saving it and close it, or cancel the
close. Saving the document works fine. Forget saving and closing (lose
changes) also works fine.

Cancel does not. Whenever I click cancel, as soon as the function exits and
the vars go out of scope I get an "Abnormal Program Termination". The same
happens if I choose to save the document before exiting, then click cancel in
the file save dialog.

Here's the stack at crash:

KERNEL32! 77e8f142()
MSVCRTD! _CxxThrowException@8 + 57 bytes
COleDispatchDriver::InvokeHelperV(long 1105, unsigned short 1, unsigned
short 0, void * 0x00000000, const unsigned char * 0x011da830 parms, char *
0x00a2f49c) line 407
COleDispatchDriver::InvokeHelper(COleDispatchDriver * const 0x00a2f598, long
1105, unsigned short 1, unsigned short 0, void * 0x00000000) line 483
_Document::Close(tagVARIANT * 0x00a2f568 {0x80020004 VT_ERROR}, tagVARIANT *
0x00a2f568 {0x80020004 VT_ERROR}, tagVARIANT * 0x00a2f568 {0x80020004
VT_ERROR}) line 3536 + 37 bytes
MyFuncThatDoesTheAutomation() line 131

Can anyone explain why this happens when cancel is clicked, and how I solve
it? Other automation methods work fine, a simple example is
objDoc.ToggleFormsDesign();
which clearly visually toggles the form designer window without problems.

Thanks, it's very frustrating as I can't really continue until I understand
what's causing this.
 
C

Cindy M -WordMVP-

Hi ...,

I don't know the MFC interface, but since you haven't gotten a response before
now, here are some thoughts, based on years of Word automation.

What you're seeing reminds me very much of what happens when, in a Word VBA
project the built-in command is "subverted" and can't complete properly. (Name a
proc the same as a built-in command - fileSaveAs for example - and your proc
takes over the command, then shows the dialog box.) It might have something to
do with the late-binding. Have you tested using early binding (not not getting
the active object), just to see if the same error occurs?

How about trying to release the objects without first trying to close the
document? (comment out that command) Does the error occur under those
circumstandes?
The problem is with an unsaved document. The code runs, and Word prompts
whether to save the document, forget saving it and close it, or cancel the
close. Saving the document works fine. Forget saving and closing (lose
changes) also works fine.

Cancel does not. Whenever I click cancel, as soon as the function exits and
the vars go out of scope I get an "Abnormal Program Termination". The same
happens if I choose to save the document before exiting, then click cancel in
the file save dialog.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top