此文件无法访问的原因和解决方案是什么?

时间:2015-10-27 22:15:56

标签: vb.net excel comexception interopservices

尝试使用基本的VB.NET Excel文件创建应用程序,从here的一些示例代码开始,我使用此代码获取错误消息:

Private Sub ButtonCreateExcelFile_Click( sender As Object,  e As EventArgs) Handles ButtonCreateExcelFile.Click
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value

    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkBook.Application.Visible = True
    xlWorkBook.Application.ScreenUpdating = False
    xlWorkSheet = xlWorkBook.Sheets("sheet1")
    'xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
    xlWorkSheet.SaveAs("C:\\misc\\vbexcel.xls")
    . . .

我第一次试过这个:

xlWorkSheet.SaveAs("C:\vbexcel.xlsx")

(没有逃脱后撤),这个(“vbexcel.xlsx”)在某种程度上被误认为是“52587100”,你可以在这里看到:

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2146827284
  HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
  HResult=-2146827284
  Message=Microsoft Office Excel cannot access the file 'C:\52587100'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
  Source=Microsoft Office Excel. . .

通过在路径中添加子文件夹和转义字符,如下所示:

xlWorkSheet.SaveAs("C:\\misc\\vbexcel.xlsx")

...我收到了类似的错误,但这次只是说它无法访问该文件(没有声称文件名被命名为“52587100”):

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2146827284
  HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
  HResult=-2146827284
  Message=The file could not be accessed. Try one of the following:

此代码有什么问题,即阻止文件可访问?

0 个答案:

没有答案