Search code examples
c++excelmsdncatia

Delete Excel sheet randomly fails


I am trying to delete Excel sheet via C++ code but randomly it fails. Here is the code:

HRESULT hr = AutoWrap(DISPATCH_METHOD, NULL, pXlSheet, L"Delete", 0);

This function returns S_OK even delete sheet failed but the sheet isn't deleted from the workbook.

Note: The system gives error sound, if delete sheet failed.


Solution

  • This error occurs because while deleting sheets sometimes warning prompt appears even deleting empty sheet.

    To suppress warning prompt i used the below snippet and it works fine.

    Code snippet:

    VARIANT vBool;
    vBool.vt=VT_BOOL;
    vBool.boolVal=FALSE;
    
    hr = AutoWrap(DISPATCH_PROPERTYPUT, NULL, _pXlApp, L"DisplayAlerts",1,vBool);
    

    This method returns S_OK:)