0

I am using this code to create an excel instance which is used for making a report

    excel = new Microsoft.Office.Interop.Excel.Application();
    excel.Visible = true;
    excel.DisplayAlerts = false;
    worKbooK = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
    worKsheeT = worKbooK.Worksheets[1]; ;
    worKsheeT.Name = "Report";
    iCountW = 2;
        
    worKsheeT.Cells[1, 1] = "S.No";
    worKsheeT.Cells[1, 2] = "Weld Stud/Nut Name";
    worKsheeT.Cells[1, 3] = "Weld Spot Clearance Status";
    worKsheeT.Cells[1, 4] = "Weld Spot Name";
    worKsheeT.Cells[1, 5] = "Coord-X";
    worKsheeT.Cells[1, 6] = "Coord-Y";
    worKsheeT.Cells[1, 7] = "Coord-Z";
    // Some Code
    // Some more code

    // No more code

After updating the data rows of this report I am using the below code for saving the excel workbook which is not letting me save the workbook.

 string filename = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) + "\\" + "Trial_StudNut.xlsx";
if (!System.IO.File.Exists(filename))
{
    worKbooK.SaveAs(filename, Excel.XlSaveAsAccessMode.xlNoChange);
}
                   

Can someone help me with what am I missing to avoid exceptions while saving the workbook?

Edit: I am getting the following

(HRESULT: 0x800A03EC Error while saving Excel file)

The code is getting inside the condition and then falling into the exception.

Progman
  • 16,827
  • 6
  • 33
  • 48
nitishhsinghhh
  • 23
  • 2
  • 10
  • You're checking to see if the file exists before saving it. As you are editing the file, I would imagine your check returns true and skips the call to `SaveAs`. Otherwise, if you are seeing an exception, please add the Exception message to your post. – Ryan Wilson Apr 14 '22 at 15:42
  • @RyanWilson My code is getting inside the condition and then failing. Exception (HRESULT: 0x800A03EC ) – nitishhsinghhh Apr 14 '22 at 15:52
  • 1
    Including the exception in the post wouldn't be a bad idea. – Ergis Apr 14 '22 at 15:55
  • 1
    @NitishSingh Perhaps this post will help you: [hresult-0x800a03ec-on-worksheet-range](https://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range) or [exception-from-hresult-0x800a03ec-error-while-saving-excel-file](https://stackoverflow.com/questions/15597490/exception-from-hresult-0x800a03ec-error-while-saving-excel-file), answer by Inspiration says to do just that, call `Activate()` on the workbook. But if that doesn't fix it, there are multiple reasons this could be happening, most outlined in the two posts I provided. – Ryan Wilson Apr 14 '22 at 16:03
  • 1
    you could try to save it to fixed name in an existing directory like: `worKbooK.SaveAs("C:\\Temp\temp.xlsx", Excel.XlSaveAsAccessMode.xlNoChange);` just to check if your `filename` is the problem – Johannes Krackowizer Apr 14 '22 at 16:14
  • @JohannesKrackowizer Thank you! Changed to Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) and it started working. – nitishhsinghhh Apr 14 '22 at 17:05
  • @RyanWilson Thank you for your detailed help. I was trying to access the public folder and due to the organization policy, I was not able to access the folder. Changed CommonDesktopDirectory to MyDocuments and boom it started working. – nitishhsinghhh Apr 15 '22 at 05:06
  • @Ergis: Thanks. Yeah updated the exception in the post. – nitishhsinghhh Apr 15 '22 at 05:58

0 Answers0