Word Automation - SaveAs

I am trying to write a simple MFC-Word Automation to save for every 1 minute. I follow this article: http://www.codeproject.com/KB/office/MSOfficeAuto.aspx

And this is what I'm trying to implement, I'm new to COM, so I think the problem is here: my VBA is generated by Word 2010:

ActiveDocument.SaveAs2 FileName:="1.docx", FileFormat:=wdFormatXMLDocument _
    , LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
     SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
    False, CompatibilityMode:=14

And my code for implementing VBA code is above:

{
    COleVariant varName(L"b.docx");
    COleVariant varFormat(L"wdFormatXMLDocument");
    COleVariant varLockCmt((BYTE)0);
    COleVariant varPass(L"");
    COleVariant varReadOnly((BYTE)0);
    COleVariant varEmbedFont((BYTE)0);
    COleVariant varSaveNativePicFormat((BYTE)0);
    COleVariant varForms((BYTE)0);
    COleVariant varAOCE((BYTE)0);
    VARIANT x;
    x.vt = VT_I4;
    x.lVal = 14;
    COleVariant varCompability(&x);;

    VARIANT result;
    VariantInit(&result);
    _hr=OLEMethod(  DISPATCH_METHOD, &result, pDocApp, L"SaveAs2",10,
                    varName.Detach(),varFormat.Detach(),varLockCmt.Detach(),varPass.Detach(),varReadOnly.Detach(),
                    varEmbedFont.Detach(),varSaveNativePicFormat.Detach(),varForms.Detach(),varAOCE.Detach(),varCompability.Detach()
                 );
}

I do not get errors from this, but it does not work.

+3
source share
3 answers

The VBA syntax uses named parameters, where the order and number of parameters do not matter. However, when calling from C ++, you need to pass the required number of parameters in the correct order.

SaveAs2 is defined as:

void SaveAs2(
    ref Object FileName,
    ref Object FileFormat,
    ref Object LockComments,
    ref Object Password,
    ref Object AddToRecentFiles,
    ref Object WritePassword,
    ref Object ReadOnlyRecommended,
    ref Object EmbedTrueTypeFonts,
    ref Object SaveNativePictureFormat,
    ref Object SaveFormsData,
    ref Object SaveAsAOCELetter,
    ref Object Encoding,
    ref Object InsertLineBreaks,
    ref Object AllowSubstitutions,
    ref Object LineEnding,
    ref Object AddBiDiMarks,
    ref Object CompatibilityMode
)

, 17 , , , CompatibilityMode, 10- ( corressponds SaveFormsData).

, :

_hr = OLEMethod(DISPATCH_METHOD, &result, pDocApp, L"SaveAs2", 2, varName.Detach(), varFormat.Detach());

, .

COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

, .

-

:

    CoInitialize(NULL);

    CLSID clsid;
    IDispatch *pWApp;
    HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);
    hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pWApp);

    hr = OLEMethod(DISPATCH_PROPERTYPUT, NULL, pWApp, L"Visible", 1, COleVariant((long)1));

    VARIANT result;
    VariantInit(&result);
    hr = OLEMethod(DISPATCH_PROPERTYGET, &result, pWApp, L"Documents", 0);
    IDispatch *pDocs = result.pdispVal;

    VARIANT result2;
    VariantInit(&result2);
    hr = OLEMethod(DISPATCH_METHOD, &result2, pDocs, L"Open", 1, COleVariant(L"D:\\Archive\\t1.docx"));
    IDispatch *pDoc = result2.pdispVal;

    VARIANT result3;
    VariantInit(&result3);
    hr = OLEMethod(DISPATCH_METHOD, &result3, pDoc, L"SaveAs2", 1, COleVariant(L"D:\\Archive\\t2.docx"));

    CoUninitialize();
+2

varFormat wdFormatXMLDocument 12 (, varCompability). , 10 "SaveAs2"?

0

, , .

Change the varFormat variable from wdFormatXMLDocumentto an integer 12(probably how you made the variable varCompability). wdFormatXMLDocumentis an enumeration WdSaveFormatand was introduced in Word 2003. There is no need to send in L"name"- just send an integer 12.

If these are previously saved documents (i.e. not new), first perform the conversion to get it in the desired format (for example, ActiveDocument.Convert)

0
source

Source: https://habr.com/ru/post/1748458/


All Articles