COleClientItem from Word Document using a document template 
Author Message
 COleClientItem from Word Document using a document template

I'm going to show some more of my ignorance here, in hopes that someone with
more OLE and Word automation experience can help.  I need to create an OLE
embedded object of a new Word document, but that document needs to be based on a
Word template (.DOT file) that is not Normal.Dot.  So far, it seems that just
doing CreateNewItem with the CLSID for "Word.Document" will give me only a
"normal" document, not allowing me to specify the template.

So, my first attempt to get around this was to create a temporary new document
based on the .DOT file I need, then use the CreateFromFile method to get my
embedded object.  This seems to work, but it gives a different result.  If I use
CreateNewItem (OleCreate) with the CLSID for Word.Document, I can QueryInterface
the m_lpObject for the _Document interface, and it succeeds.  But if I use
CreateFromFile, the m_lpObject returns E_NOINTERFACE:

CMyCntrItem * pItem = NULL ;
TRY
{
    Word::DocumentsPtr pDocuments = theApp.m_pWordApp->Documents ;
    COleVariant vtTemplate( theApp.GetNewDocTemplate() ) ;
    m_pWordDoc = pDocuments->Add( &vtTemplate ) ;

    TCHAR szPath[_MAX_PATH] ;
    CString strDocName ;
    if ( ( ::GetTempPath( _MAX_PATH, szPath ) > 0 )
      && ( ::GetTempFileName( szPath, _T("WAD"), 0, strDocName.GetBuffer(
_MAX_PATH ) ) > 0 )
       )
    {
        strDocName.ReleaseBuffer() ;
        COleVariant vtDocName( strDocName ) ;
        if ( m_pWordDoc->SaveAs( &vtDocName )  == S_OK )
        {
            m_pWordDoc->Close() ;

            pItem = new CMyCntrItem( this ) ;
            ASSERT_VALID(pItem) ;
            CLSID clsidWordDoc ;
            ::CLSIDFromProgID( L"Word.Document", &clsidWordDoc ) ;

            if ( pItem->CreateFromFile( strDocName, clsidWordDoc )
              && ( pItem->m_lpObject != NULL )
               )
            {
                ASSERT_VALID(pItem) ;
                // attach to _Document interface
**** THIS FAILS because the m_lpObject doesn't have the _Document interface...
why?
                m_pWordDoc = pItem->m_lpObject ;
            }
            else
                AfxMessageBox( IDS_CANNOT_CREATE, MB_OK | MB_ICONSTOP ) ;

            ::DeleteFile( strDocName ) ;



Tue, 25 May 2004 07:26:29 GMT  
 COleClientItem from Word Document using a document template
Scot,

I create fresh Word-docs using other templates than normal.dot thus:

 _Application oWord;
 Documents  oDocs;
 _Document  oDoc;

 COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
    vtTemplate;

 if (!oWord.CreateDispatch("Word.Application"))
  return FALSE;

 vtTemplate = m_strOtherDotFile;

 // - - - - - - - - - -
 // Add a new document
 // - - - - - - - - - -

 oDocs = oWord.GetDocuments();
 oDoc = oDocs.Add(vtTemplate,vtOptional);

Johan Rosengren



Quote:
> I'm going to show some more of my ignorance here, in hopes that someone
with
> more OLE and Word automation experience can help.  I need to create an OLE
> embedded object of a new Word document, but that document needs to be
based on a
> Word template (.DOT file) that is not Normal.Dot.  So far, it seems that
just
> doing CreateNewItem with the CLSID for "Word.Document" will give me only a
> "normal" document, not allowing me to specify the template.

> So, my first attempt to get around this was to create a temporary new
document
> based on the .DOT file I need, then use the CreateFromFile method to get
my
> embedded object.  This seems to work, but it gives a different result.  If
I use
> CreateNewItem (OleCreate) with the CLSID for Word.Document, I can
QueryInterface
> the m_lpObject for the _Document interface, and it succeeds.  But if I use
> CreateFromFile, the m_lpObject returns E_NOINTERFACE:

> CMyCntrItem * pItem = NULL ;
> TRY
> {
>     Word::DocumentsPtr pDocuments = theApp.m_pWordApp->Documents ;
>     COleVariant vtTemplate( theApp.GetNewDocTemplate() ) ;
>     m_pWordDoc = pDocuments->Add( &vtTemplate ) ;

>     TCHAR szPath[_MAX_PATH] ;
>     CString strDocName ;
>     if ( ( ::GetTempPath( _MAX_PATH, szPath ) > 0 )
>       && ( ::GetTempFileName( szPath, _T("WAD"), 0, strDocName.GetBuffer(
> _MAX_PATH ) ) > 0 )
>        )
>     {
>         strDocName.ReleaseBuffer() ;
>         COleVariant vtDocName( strDocName ) ;
>         if ( m_pWordDoc->SaveAs( &vtDocName )  == S_OK )
>         {
>             m_pWordDoc->Close() ;

>             pItem = new CMyCntrItem( this ) ;
>             ASSERT_VALID(pItem) ;
>             CLSID clsidWordDoc ;
>             ::CLSIDFromProgID( L"Word.Document", &clsidWordDoc ) ;

>             if ( pItem->CreateFromFile( strDocName, clsidWordDoc )
>               && ( pItem->m_lpObject != NULL )
>                )
>             {
>                 ASSERT_VALID(pItem) ;
>                 // attach to _Document interface
> **** THIS FAILS because the m_lpObject doesn't have the _Document
interface...
> why?
>                 m_pWordDoc = pItem->m_lpObject ;
>             }
>             else
>                 AfxMessageBox( IDS_CANNOT_CREATE, MB_OK | MB_ICONSTOP ) ;

>             ::DeleteFile( strDocName ) ;



Wed, 26 May 2004 23:24:07 GMT  
 COleClientItem from Word Document using a document template
Yes, Johan.  Thanks for replying.  I used the same dispatch methods you
described as well, and it works.  My problem comes from then trying to create a
COleClientItem from a Word document object that I've opened this way.  When I
use CreateNewItem, I have no trouble connecting to the IDispatch interface.  But
when I create from a file, by opening it with a template, the document object
doesn't seem to provide the _Document or IDispatch interfaces, and I can't
connect my wrapper class to it.


Thu, 27 May 2004 09:33:31 GMT  
 COleClientItem from Word Document using a document template
In case anyone is curious, the apparent answer to my problem was that the
embedded object was not yet running in its server when CreateFromFile is used.
(Evidently, CreateNewItem runs the object right away).  All I had to do was call
        pItem->Run() ;
before attempting to connect to the _Document interface.


Thu, 27 May 2004 19:06:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to Embed Microsoft Word Document without using document view architecture

2. Accessing a documents version information (such as Word, Excel or other Office document)

3. Creating a thumbnail of a word document/template

4. Opening word documents using C#

5. Using c# to parse Word document

6. Real difficulty with Word document Open paramters - using #import

7. to print a Word document using VC++

8. Printing a word document using C++

9. How to open a word document using VC++

10. Creating word 97 document with text boxes using automation

11. Automation of whole Documents using MFC and Word/Excel

12. create a word document using only text

 

 
Powered by phpBB® Forum Software