Click here to Skip to main content
15,559,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my application I am using Excel to fill the data from the database. When I am saving the file, if the file is already exist it shows would you want to replace it. When I click the "Yes" button it works fine When I click the "NO" button it throws an error as "System.Runtime.InteropServices.COMException (0x800A03EC):
Exception from HRESULT: 0x800A03EC"
. Can any one help me to solve this problem. Here is the code for my application:

excelWorkbook.SaveAs(@"D:\RiteFile\" + filename + ".xlsm");//After this I am getting the Error
string path = @"D:\RiteFile\" + filename + ".xlsm";
                excelApp.DisplayAlerts = false;               
                excelApp.UserControl = false;
                excelWorkbook.Close();
                excelApp.Workbooks.Close();
                excelApp.Quit();                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                excelWorkbook = null;
                excelApp = null;
                System.Diagnostics.Process.Start(path);

            }
        }
        catch (Exception ex)
        {
            UserUtil.Message(ex.Message, this);
            excelApp.DisplayAlerts = false;
            excelApp.UserControl = false;
            //excelWorkbook.Close();
            excelApp.Workbooks.Close();
            excelApp.Quit();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
            //excelWorkbook = null;
            excelApp = null;

        }


Thanks in Advance
Posted

If you have more than 256 columns of data, try saving as a CSV file instead.

EDIT (after you stated it only happens if you click NO/CANCEL) =================

The SaveAs form will return a result (probably OK or CANCEL). What you need to do is use that return value, something like this:

C#
string path = "";
if (excelWorkbook != null)
{
    DialogResult result = excelWorkbook.SaveAs(...);
    if (result == DialogResult.OK)
    {
        string path = @"D:\RiteFile\" + filename + ".xlsm";
    }
    excelApp.DisplayAlerts = false;
    excelApp.UserControl = false;
    excelWorkbook.Close();
    excelApp.Workbooks.Close();
    excelApp.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
    excelWorkbook = null;
    excelApp = null;
    if (!string.IsNullOrEmpty(path))
    {
        System.Diagnostics.Process.Start(path);
    }
}


Keep in mind that I don't know for sure what the return type is, and the code above is merely an example.
 
Share this answer
 
v2
Why don't you try setting DisplayAlerts = false before calling the SaveAs method?
 
Share this answer
 
This could be access permission error.

1. check access permission for D:\RiteFile.
2. right click RiteFile directory--> Properties --> Security tab

give full access to Users group and ASP.NET user.
 
Share this answer
 
Comments
Pravin Patil, Mumbai 23-Aug-11 5:56am    
I don't think this is the access problem, because OP can save the file when he clicks YES as he has mentioned in the question.
Member 7946563 23-Aug-11 6:38am    
This is not access problem. The problem is when I click the Yes button it works fine. If I click No or Cancel Button it throws Error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900