CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2013
    Posts
    6

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Automation MS Word in Active-X container. Please help to properly create the obje

    Have a look at this essay:
    Surviving the Release Version
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2013
    Posts
    6

    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?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Automation MS Word in Active-X container. Please help to properly create the obje

    Quote Originally Posted by cupuyc View Post
    ...
    It works in debug version but falls in release.
    Quote Originally Posted by cupuyc View Post
    Thanks. But i`d like to know: did i write correct code?
    How can this code be correct if it fails in Release build?
    Besides this code produces the memory leaks: you create CXContainerCntrItem* pItem with new, but do not delete it!
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2013
    Posts
    6

    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.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Automation MS Word in Active-X container. Please help to properly create the obje

    Quote Originally Posted by cupuyc View Post
    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 View Post
    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!
    Victor Nijegorodov

  7. #7
    Join Date
    Oct 2013
    Posts
    6

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured