Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 824545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:06:55+00:00 2026-05-15T03:06:55+00:00

I try to write a simple MFC – Word Automation to save for every

  • 0

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/MSOfficeAuto.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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:06:56+00:00Added an answer on May 15, 2026 at 3:06 am

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.