Automation in MFC 
Author Message
 Automation in MFC

Hi

I would like to save Word document to RTF using Word engine
Below is my function.
When document is saving then throw an exception "server throw an exception"
What i'm doing wrong. Thanks for help.

int CAutoWord::SaveAsRTF(char *strFilePath)
{
    VARIANT varRetVal;
    EXCEPINFO excepInfo;
    VARIANTARG varg;

    varg.vt = VT_BSTR;
    varg.bstrVal = _bstr_t(strFilePath);

    DISPPARAMS dpOpen = { &varg, NULL, 1, 0 };
    DISPID dispOpenID;
    LPOLESTR szOpenDoc = L"Open";
    HRESULT hr = m_pDocuments->GetIDsOfNames(IID_NULL, &szOpenDoc, 1,
    LOCALE_SYSTEM_DEFAULT, &dispOpenID);

    hr = m_pDocuments->Invoke(dispOpenID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
    DISPATCH_METHOD, &dpOpen, &varRetVal, &excepInfo, NULL);

    if (FAILED(hr))
    {
        MessageBox("Error opening the document\n");
        Destroy();
        return -1;
    }

    IDispatch* pDocument = varRetVal.pdispVal;
    CString sFilePath(strFilePath);
    sFilePath.Replace(_T(".doc"),_T(".rtf"));

    VARIANTARG vargFname;
    vargFname.vt = VT_BSTR;
    vargFname.bstrVal = _bstr_t(sFilePath.GetString());

    VARIANTARG vargFileFormat;
    vargFileFormat.vt = VT_INT;
    vargFileFormat.intVal = 103;

    VARIANTARG vargLockComments;
    vargLockComments.vt = VT_BOOL;
    vargLockComments.boolVal = VARIANT_TRUE;

    VARIANTARG vargPassword;
    vargPassword.vt = VT_BSTR;
    vargPassword.bstrVal = _bstr_t("");

    VARIANTARG vargAddToRecentFiles;
    vargAddToRecentFiles.vt = VT_BOOL;
    vargAddToRecentFiles.boolVal = VARIANT_TRUE;

    VARIANTARG vargWritePassword;
    vargWritePassword.vt = VT_BSTR;
    vargWritePassword.bstrVal = _bstr_t("");

    VARIANTARG vargReadOnlyRecommended;
    vargReadOnlyRecommended.vt = VT_BOOL;
    vargReadOnlyRecommended.boolVal = VARIANT_FALSE;

    VARIANTARG vargEmbedTrueTypeFonts;
    vargEmbedTrueTypeFonts.vt = VT_BOOL;
    vargEmbedTrueTypeFonts.boolVal = VARIANT_FALSE;

    VARIANTARG vargSaveNativePictureFormat;
    vargSaveNativePictureFormat.vt = VT_BOOL;
    vargSaveNativePictureFormat.boolVal = VARIANT_FALSE;

    VARIANTARG vargSaveFormsData;
    vargSaveFormsData.vt = VT_BOOL;
    vargSaveFormsData.boolVal = VARIANT_FALSE;

    VARIANTARG vargSaveAsAOCELetter;
    vargSaveAsAOCELetter.vt = VT_BOOL;
    vargSaveAsAOCELetter.boolVal = VARIANT_FALSE;

    VARIANTARG argArray [11];
    argArray[10]=vargFname;
    argArray[ 9]=vargFileFormat;
    argArray[ 8]=vargLockComments;
    argArray[ 7]=vargPassword;
    argArray[ 6]=vargAddToRecentFiles;
    argArray[ 5]=vargWritePassword;
    argArray[ 4]=vargReadOnlyRecommended;
    argArray[ 3]=vargEmbedTrueTypeFonts;
    argArray[ 2]=vargSaveNativePictureFormat;
    argArray[ 1]=vargSaveFormsData;
    argArray[ 0]=vargSaveAsAOCELetter;

    DISPPARAMS dpSave = { argArray, NULL, 11, 0 };
    DISPID dispSaveID;
    LPOLESTR szSaveDoc = L"SaveAs";
    hr = pDocument->GetIDsOfNames(IID_NULL, &szSaveDoc, 1,
    LOCALE_SYSTEM_DEFAULT, &dispSaveID);

    hr = pDocument->Invoke(dispSaveID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
    DISPATCH_METHOD, &dpSave, &varRetVal, NULL, NULL);

    if (FAILED(hr))
    {
        MessageBox("The document could not be saved\n");
        Destroy();
        return -1;
    }
    DISPPARAMS dpClose = { NULL, NULL, 0, 0 };
    DISPID dispCloseID;
    LPOLESTR szCloseDoc = L"Close";
    hr = pDocument->GetIDsOfNames(IID_NULL, &szCloseDoc, 1,
    LOCALE_SYSTEM_DEFAULT, &dispCloseID);
    hr = pDocument->Invoke(dispCloseID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
    DISPATCH_METHOD, &dpClose, &varRetVal, &excepInfo, NULL);

    return 0;

Quote:
}

K.
--
 --------------------------------------------
 Sanat suussani sulavat, puhe'et putoelevat,
 kielelleni kerki?v?t, hampahilleni hajoovat.
 --------------------------------------------

--
Serwis Usenet w portalu Gazeta.pl -> http://www.*-*-*.com/



Tue, 13 Sep 2005 13:10:39 GMT  
 Automation in MFC
I've used this in the past:

{
    CString         strFilename,
                    strText("c:\\file.rtf");
    long            lType;
    _Application    app;
    Documents       docs;
    _Document       doc;
    COleVariant     vtOptional((long) DISP_E_PARAMNOTFOUND, VT_ERROR),
                    vtFalse((short) FALSE),
                    vtTrue((short) TRUE),
                    vtDoc(0L),
                    vtTxt(2L),
                    vtRtf(6L),
                    vtHtml(8L);

    if (app.CreateDispatch("Word.Application") == TRUE)
    {
        docs = app.GetDocuments();

        m_ebFilename.GetWindowText(strFilename); // some .DOC file

        doc = docs.Open(COleVariant(strFilename), vtOptional, vtOptional,
vtOptional, vtOptional, vtOptional, vtOptional, vtOptional, vtOptional,
vtOptional, vtOptional, vtOptional);

        lType = doc.GetSaveFormat();

        doc.SaveAs(COleVariant(strText), vtRtf, vtOptional, vtOptional,
vtOptional, vtOptional, vtOptional, vtOptional, vtOptional, vtOptional,
vtOptional);

        app.Quit(vtFalse, vtOptional, vtOptional);
    }

Quote:
}



Quote:
> Hi

> I would like to save Word document to RTF using Word engine
> Below is my function.
> When document is saving then throw an exception "server throw an
exception"
> What i'm doing wrong. Thanks for help.

> int CAutoWord::SaveAsRTF(char *strFilePath)
> {
>     VARIANT varRetVal;
>     EXCEPINFO excepInfo;
>     VARIANTARG varg;

>     varg.vt = VT_BSTR;
>     varg.bstrVal = _bstr_t(strFilePath);

>     DISPPARAMS dpOpen = { &varg, NULL, 1, 0 };
>     DISPID dispOpenID;
>     LPOLESTR szOpenDoc = L"Open";
>     HRESULT hr = m_pDocuments->GetIDsOfNames(IID_NULL, &szOpenDoc, 1,
>     LOCALE_SYSTEM_DEFAULT, &dispOpenID);

>     hr = m_pDocuments->Invoke(dispOpenID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
>     DISPATCH_METHOD, &dpOpen, &varRetVal, &excepInfo, NULL);

>     if (FAILED(hr))
>     {
>         MessageBox("Error opening the document\n");
>         Destroy();
>         return -1;
>     }

>     IDispatch* pDocument = varRetVal.pdispVal;
>     CString sFilePath(strFilePath);
>     sFilePath.Replace(_T(".doc"),_T(".rtf"));

>     VARIANTARG vargFname;
>     vargFname.vt = VT_BSTR;
>     vargFname.bstrVal = _bstr_t(sFilePath.GetString());

>     VARIANTARG vargFileFormat;
>     vargFileFormat.vt = VT_INT;
>     vargFileFormat.intVal = 103;

>     VARIANTARG vargLockComments;
>     vargLockComments.vt = VT_BOOL;
>     vargLockComments.boolVal = VARIANT_TRUE;

>     VARIANTARG vargPassword;
>     vargPassword.vt = VT_BSTR;
>     vargPassword.bstrVal = _bstr_t("");

>     VARIANTARG vargAddToRecentFiles;
>     vargAddToRecentFiles.vt = VT_BOOL;
>     vargAddToRecentFiles.boolVal = VARIANT_TRUE;

>     VARIANTARG vargWritePassword;
>     vargWritePassword.vt = VT_BSTR;
>     vargWritePassword.bstrVal = _bstr_t("");

>     VARIANTARG vargReadOnlyRecommended;
>     vargReadOnlyRecommended.vt = VT_BOOL;
>     vargReadOnlyRecommended.boolVal = VARIANT_FALSE;

>     VARIANTARG vargEmbedTrueTypeFonts;
>     vargEmbedTrueTypeFonts.vt = VT_BOOL;
>     vargEmbedTrueTypeFonts.boolVal = VARIANT_FALSE;

>     VARIANTARG vargSaveNativePictureFormat;
>     vargSaveNativePictureFormat.vt = VT_BOOL;
>     vargSaveNativePictureFormat.boolVal = VARIANT_FALSE;

>     VARIANTARG vargSaveFormsData;
>     vargSaveFormsData.vt = VT_BOOL;
>     vargSaveFormsData.boolVal = VARIANT_FALSE;

>     VARIANTARG vargSaveAsAOCELetter;
>     vargSaveAsAOCELetter.vt = VT_BOOL;
>     vargSaveAsAOCELetter.boolVal = VARIANT_FALSE;

>     VARIANTARG argArray [11];
>     argArray[10]=vargFname;
>     argArray[ 9]=vargFileFormat;
>     argArray[ 8]=vargLockComments;
>     argArray[ 7]=vargPassword;
>     argArray[ 6]=vargAddToRecentFiles;
>     argArray[ 5]=vargWritePassword;
>     argArray[ 4]=vargReadOnlyRecommended;
>     argArray[ 3]=vargEmbedTrueTypeFonts;
>     argArray[ 2]=vargSaveNativePictureFormat;
>     argArray[ 1]=vargSaveFormsData;
>     argArray[ 0]=vargSaveAsAOCELetter;

>     DISPPARAMS dpSave = { argArray, NULL, 11, 0 };
>     DISPID dispSaveID;
>     LPOLESTR szSaveDoc = L"SaveAs";
>     hr = pDocument->GetIDsOfNames(IID_NULL, &szSaveDoc, 1,
>     LOCALE_SYSTEM_DEFAULT, &dispSaveID);

>     hr = pDocument->Invoke(dispSaveID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
>     DISPATCH_METHOD, &dpSave, &varRetVal, NULL, NULL);

>     if (FAILED(hr))
>     {
>         MessageBox("The document could not be saved\n");
>         Destroy();
>         return -1;
>     }
>     DISPPARAMS dpClose = { NULL, NULL, 0, 0 };
>     DISPID dispCloseID;
>     LPOLESTR szCloseDoc = L"Close";
>     hr = pDocument->GetIDsOfNames(IID_NULL, &szCloseDoc, 1,
>     LOCALE_SYSTEM_DEFAULT, &dispCloseID);
>     hr = pDocument->Invoke(dispCloseID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
>     DISPATCH_METHOD, &dpClose, &varRetVal, &excepInfo, NULL);

>     return 0;
> }

> K.
> --
>  --------------------------------------------
>  Sanat suussani sulavat, puhe'et putoelevat,
>  kielelleni kerki?v?t, hampahilleni hajoovat.
>  --------------------------------------------

> --
> Serwis Usenet w portalu Gazeta.pl -> http://www.gazeta.pl/usenet/



Wed, 14 Sep 2005 18:03:52 GMT  
 Automation in MFC
Thanks very much i'll try

K.
--
 --------------------------------------------
 Sanat suussani sulavat, puhe'et putoelevat,
 kielelleni kerki?v?t, hampahilleni hajoovat.
 --------------------------------------------

--
Serwis Usenet w portalu Gazeta.pl -> http://www.gazeta.pl/usenet/



Thu, 15 Sep 2005 18:26:23 GMT  
 Automation in MFC

Quote:

>     _Application    app;
>     Documents       docs;
>     _Document       doc;

Hi

I have one more question.
what kind of types, classes are _Application, Documents, _Document,
becouse i can't find it in MSDN, witch headers and libraries i should use.

Krzysztof Jankowski
--
 --------------------------------------------
 Sanat suussani sulavat, puhe'et putoelevat,
 kielelleni kerki?v?t, hampahilleni hajoovat.
 --------------------------------------------

--
Serwis Usenet w portalu Gazeta.pl -> http://www.gazeta.pl/usenet/



Thu, 15 Sep 2005 20:46:35 GMT  
 Automation in MFC
Those come from the Word type library (msword9.olb).


Quote:

> >     _Application    app;
> >     Documents       docs;
> >     _Document       doc;

> Hi

> I have one more question.
> what kind of types, classes are _Application, Documents, _Document,
> becouse i can't find it in MSDN, witch headers and libraries i should use.



Fri, 16 Sep 2005 16:40:05 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Add Automation to MFC application

2. PPT Automation with MFC HELP!!

3. Office Automation from MFC

4. Automation in MFC

5. Automation and MFC

6. Help! Excel automation with MFC and OLE

7. MFC/Word automation

8. MFC/Word communication (automation)

9. Saving and retrieving Ole-Automation objects from Dao and MFC using CLongBinary class

10. Help converting MFC Automation interface to ATL

11. Help converting an MFC automation interface to ATL

12. registering and unregistering an MFC automation server.

 

 
Powered by phpBB® Forum Software