none
How to Acess Undo command of excel in mfc automation. RRS feed

  • General discussion

  • Hi,

      I am automating excel  using mfc wrapper classes. (As in the link http://support.microsoft.com/kb/308292) . Able to invoke the cut, copy and paste commands of excel in the VC++ application. But not able to access the Unlimited undo command of excel in VC++ program. How this can be achieved?

    Regards

    Pradeep

    Tuesday, August 19, 2014 5:14 AM

All replies

  • Hello Pradeep,

    You can use the ExecuteMso method of the CommandBars class (see the corresponding property of the Application class to get an instance of the class). You just need to pass the idMso values (for example - Cut, Copy, Paste) to run a corresponding command in the UI. See Office 2013 Help Files: Office Fluent User Interface Control Identifiers for more idMso values.

    Tuesday, August 19, 2014 10:36 AM
  • Hi Eugene,

    Thanks for your reply.

      I had used the ExcuteMso method in my VC++(10.0) appliaction with Microsoft excel 2007  as shown in below code snippet. It is giving the exception.


    CApplication app;
    BOOL CExcelOperation::InitilizeTheExcelApp()
    {    
        // Start Excel and get an Application object.
       if(!app.CreateDispatch(TEXT("Excel.Application")))
       {
          AfxMessageBox(TEXT("Couldn't start Excel and get Application object."));
          return FALSE;
       }
       return TRUE;

    }

    ....

    .....

    //OpentheSheets and required sheet number

    .....

    /***Write some data to excel*****/

    void CExcelOperation::WirteEntireRow(CString Data,CString Row)
    {
        range = sheet.get_Range(COleVariant(Row), COleVariant(Row));
        range = range.get_EntireRow();
        range.put_Value2(COleVariant(Data));
    }

    .......

    .......

    /** Do some other excel write operations*****/

    .........

    .........

    /****DO the Unlimited  Undo  command****/

    void CExcelOperation::UnDoLast()
    {
        CCommandBars objCmdBar = app.get_CommandBars();    

        TRY
        {
           objCmdBar.ExecuteMso(L"Undo");/***Giving the Exception Here**/
        }
        CATCH_ALL(e)
        {
            AfxMessageBox(L"Exception");/*Coming Here*/
        }
        END_CATCH_ALL     


    }









    Thursday, August 28, 2014 9:42 AM
  • Hi Eugene,

    Thanks for your reply.

      I had used the ExcuteMso method in my VC++(10.0) appliaction with Microsoft excel 2007  as shown in below code snippet. It is giving the exception.


    CApplication app;
    BOOL CExcelOperation::InitilizeTheExcelApp()
    {    
        // Start Excel and get an Application object.
       if(!app.CreateDispatch(TEXT("Excel.Application")))
       {
          AfxMessageBox(TEXT("Couldn't start Excel and get Application object."));
          return FALSE;
       }
       return TRUE;

    }

    ....

    .....

    //OpentheSheets and required sheet number

    .....

    /***Write some data to excel*****/

    void CExcelOperation::WirteEntireRow(CString Data,CString Row)
    {
        range = sheet.get_Range(COleVariant(Row), COleVariant(Row));
        range = range.get_EntireRow();
        range.put_Value2(COleVariant(Data));
    }

    .......

    .......

    /** Do some other excel write operations*****/

    .........

    .........

    /****DO the Unlimited  Undo  command****/

    void CExcelOperation::UnDoLast()
    {
        CCommandBars objCmdBar = app.get_CommandBars();    

        TRY
        {
           objCmdBar.ExecuteMso(L"Undo");/***Giving the Exception Here**/
        }
        CATCH_ALL(e)
        {
            AfxMessageBox(L"Exception");/*Coming Here*/
        }
        END_CATCH_ALL     


    }










    Thursday, August 28, 2014 9:47 AM
  • Undo is a gallery control. It can't be run using the ExecuteMso method.

    Instead, you need to use the Undo method of the Application class which cancels the last user-interface action.

    Thursday, August 28, 2014 11:42 AM
  • Hi Eugene,

    Thanks for the reply.

       As per your suggestion checked with Undo method of  Application Class. But it will Undo Last one action. Writing the Excel using the COleSafeArray (Initially filling the  values to this array) and then writing to excel sheet using put_Value2() method .   Need to do the Undo for unlimited(All Last actions As in MS Excel 2007). Can u suggest the some solution?


    Thanks

    Regards

    Pradeep


    Friday, September 12, 2014 12:50 PM