none
Why can't an Excel file be deleted sometimes? RRS feed

  • Question

  • I use C# to edit an Excel WorkBook, then export the PDF file based on it, then close the WorkBook and exit Excel application, and finally delete the WorkBook file.
    However, sometimes this WorkBook cannot be deleted. What should I do?

    //...
    workBook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, destPdfFile);
    workBook.Close(null, null, null);                
    XlsApp.DisplayAlerts = true;
    XlsApp.Visible = true;
    XlsApp.ScreenUpdating = true;               
    
    XlsApp.Quit();
    Marshal.ReleaseComObject(XlsApp);
    GC.Collect();
    
    File.Delete(destXlsFile);//error message:HRESULT:0x800A03EC


    Thursday, March 22, 2018 3:52 AM

Answers

  • Hi Ashidacchi,

    I initially thought it was an error to delete this Excel file, but infact it was an error when exporting the PDF. Mybe I should raise a new thread.
    I just found the root cause of this problem: missing the SaveAsPDFOrXPS Add-in. Anyway, thank you very much for your reply.



    • Marked as answer by leon1526 Saturday, March 24, 2018 6:48 AM
    Saturday, March 24, 2018 6:48 AM

All replies

  • Hi leon1526,

    I've found an article about the error 0x800A03EC.
    I am getting HRESULT: 0x800A03EC on Worksheet.range method. Number of rows are more than 70K. Office 2007.
    https://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range

    It says:
    --------------------------
    This problem occurs if you are using a backwards compatible sheet (a .xls) instead of a .xlsx

    To allow sheets to be opened in pre office 2007 version it can't contain more than 65k rows. You can check the number of rows in your sheet by using ctrl+arrowdown till you hit the bottom. If you try to get a range larger than that number of rows it will create an error
    --------------------------

    Did you use an old format Excel file (.xls) when the error occurred?
    Please check if there are difference between using .xls and .xlsx file. 
    Regards,

    Ashidacchi >> http://hokusosha.com/

    Thursday, March 22, 2018 5:34 AM
  • Hi Ashidacchi,

    Thank you very much for your reply.
    I'm very sorry that my previous description was wrong. The actual error was occurred by exporting the PDF, and the Excel file is in .xlsx format.


    Thursday, March 22, 2018 7:36 AM
  • Hi leon1526,

    Thank you for reply.
    I'd like to confirm:
    (1) what is your programming language? Is it Excel VBA?
    (2) what application does your code call? Is it Excel?
    (sorry, my poor English)

    Regards,

    Ashidacchi >> http://hokusosha.com/

    Thursday, March 22, 2018 7:41 AM
  • Hello lenon1526,

    >>The actual error was occurred by exporting the PDF, and the Excel file is in .xlsx format.

    Do you mean that the error occur on "workBook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, destPdfFile);"? Is there any detail error message excepts 0x800A03EC?

    How do you define destPdfFile? If you print out the destPdfFile while the error occur, is the destPdfFile an valid pdf filename?

    Best Regards,

    Terry

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 23, 2018 5:43 AM
  • Hi leon1526,

    A variable "destPdfFile" is used in your code:
      workBook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, destPdfFile);

    Do you give it a file name with full path, like "C:\temp\abc.pdf"?

    And I would like to confirm. Does your subject/title "Why can't an Excel file be deleted sometimes?" correspond with body text?

    Regards,

    Ashidacchi >> http://hokusosha.com/


    • Edited by Ashidacchi Friday, March 23, 2018 6:11 AM
    Friday, March 23, 2018 6:09 AM
  • Hi Terry,

    This program can export pdf file on my computer and some other computers most of the time. I only have the problem mentioned above occasionally.
    However, on some other computers, it is impossible to export pdf files at all.
    I'm sure the pdf file name is okay.

    Friday, March 23, 2018 3:28 PM
  • Hi Ashidacchi ,

    Thanks for your reply.
    I used C# to develop an Excel report and then converted it to a pdf file.


    Friday, March 23, 2018 3:35 PM
  • Hi leon1526,

    Does your thread title (heading) express your body text (contents)?

    If your issue is inability to export PDF file in some computers, please check if output folder exists in that computer, or check if that computer has permission to access to a specified folder. 

    Regards,

    Ashidacchi -- http://hokusosha.com/

    Friday, March 23, 2018 11:22 PM
  • Hi Ashidacchi,

    I initially thought it was an error to delete this Excel file, but infact it was an error when exporting the PDF. Mybe I should raise a new thread.
    I just found the root cause of this problem: missing the SaveAsPDFOrXPS Add-in. Anyway, thank you very much for your reply.



    • Marked as answer by leon1526 Saturday, March 24, 2018 6:48 AM
    Saturday, March 24, 2018 6:48 AM