Automation MS Word in Active-X container. Please help to properly create the object.
Good day.
I`m working with MS Visual Studio 6. Trying to connect Word server with my window.
This is call server in my class CXContainerView, inherit from CScrollView:
Code:
void CXContainerView::CreateWord()
{
::CoInitialize(NULL);
_Document doc;
_Application m_app;
LPDISPATCH lpDisp;
CLSID clsid;
COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CXContainerDoc* pDoc = GetDocument();
CXContainerCntrItem* pItem = new CXContainerCntrItem(pDoc);
HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);
if(FAILED(hr))
{
::MessageBox(NULL, "CLSIDFromProgID() failed", "Error",0x10010);
GetDocument()->m_pChild->PostMessage(WM_CLOSE);
*(GetDocument()->m_succeed) = false;
return;
}
if(!pItem->CreateFromFile(pDoc->m_RtfTemplate.GetBuffer()))
{
AfxMessageBox("Ошибка при открытии файла MS Word", MB_ICONSTOP);
GetDocument()->m_pChild->PostMessage(WM_CLOSE);
*(GetDocument()->m_succeed) = false;
return;
}
else
pItem->DoVerb(OLEIVERB_SHOW, this);
ASSERT_VALID(pItem);
m_pSelection = pItem;
pDoc->UpdateAllViews(NULL);
pDoc->m_pChild->ShowWindow(SW_HIDE);
doc.CreateDispatch("Word.Document");
doc.AttachDispatch(pItem->GetIDispatch(),TRUE);
m_app = doc.GetApplication(); // <- i want to get pointer to the Word application and its falls
}
It works in debug version but falls in release. Feels my fifth point, what I call it in the wrong way. Prompt how to do it right please.
Re: Automation MS Word in Active-X container. Please help to properly create the obje
Re: Automation MS Word in Active-X container. Please help to properly create the obje
Thanks. But i`d like to know: did i write correct code?
Re: Automation MS Word in Active-X container. Please help to properly create the obje
Quote:
Originally Posted by
cupuyc
...
It works in debug version but falls in release.
Quote:
Originally Posted by
cupuyc
Thanks. But i`d like to know: did i write correct code?
How can this code be correct if it fails in Release build? :confused:
Besides this code produces the memory leaks: you create CXContainerCntrItem* pItem with new, but do not delete it!
Re: Automation MS Word in Active-X container. Please help to properly create the obje
I mean: did i write all correct with com technology? may be i invoke word-server incorrectly.
and i delete pItem later.
Re: Automation MS Word in Active-X container. Please help to properly create the obje
Quote:
Originally Posted by
cupuyc
I mean: did i write all correct with com technology? may be i invoke word-server incorrectly.
I don't know.
However, some words about your code:
Quote:
Originally Posted by
cupuyc
Code:
void CXContainerView::CreateWord()
{
::CoInitialize(NULL);
_Document doc;
_Application m_app;
LPDISPATCH lpDisp;
CLSID clsid;
COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
...
m_app = doc.GetApplication(); // <- i want to get pointer to the Word application and its falls
}
You didn't explain how it fails.
Besides, your m_app instance is defined locally inside CreateWord() method, and it cannot be used after this method returns. So this "get pointer to the Word application" does not make any sense!
Re: Automation MS Word in Active-X container. Please help to properly create the obje
I solved the problem. This source helped me: http://support.microsoft.com/kb/238611/en-us