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.
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.