开发者

Word automation - SaveAs

开发者 https://www.devze.com 2023-01-02 18:50 出处:网络
I try to write a simple MFC - Word Automation to save for every 1 minute. I follow this article : http://www.codeproject.com/KB/office/MSOffic开发者_JAVA百科eAuto.aspx

I try to write a simple MFC - Word Automation to save for every 1 minute. I follow this article : http://www.codeproject.com/KB/office/MSOffic开发者_JAVA百科eAuto.aspx

And this is what Im trying to implement , I'm new to COM so I think there's problem 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 to implement VBA code 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 get no error from this one, but it doesn't work.


The VBA syntax uses named parameters where the order and count of the parameters do not matter. However, when calling from C++ you need to pass the required number of parameters in the right 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
)

So, it has 17 parameters which means you should specify them all if you are going to pass CompatibilityMode, which was specified as 10th parameter in your example (that corressponds to SaveFormsData).

If you do not need all the parameters, or just for testing, you can try a simpler code:

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

If you need the rest of the parameters, you need to pass all the parameters up to the parameter you need set. In that case, you can pass

COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

for the parameters you do not like to set.

Edit - Test Code

This works for me:

    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();


Change your variable varFormat from wdFormatXMLDocument to an integer of 12 (probably like you've done the varCompability variable). Also, what's the 10 for after "SaveAs2"?


Guess I'll start over since you opened the bounty.

Change your variable varFormat from wdFormatXMLDocument to an integer of 12 (probably like you've done the varCompability variable). wdFormatXMLDocument is an enum of WdSaveFormat and was introduced in Word 2003. There is no need to send in an L"name" - just send in the integer of 12.

If these are previously saved documents (i.e. not new ones), perform a conversion first to get it to the right format (like ActiveDocument.Convert)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号