WinForms COM Exception opening Excel workbook

流过昼夜 提交于 2019-12-18 09:24:33

问题


I'm writing a very basic WinForms app using VB.Net, everything is going smoothly until I try and open an Excel file that I've written to the tempLocation via a Stream object (The workbook is in my project resources and the build action is set to "Embedded Resource")

Here's the code that causes the error:

Dim xlWBTemp As Excel.Workbook
Dim xlApp As Excel.Application
'// Below resolves to "C:\Users\MacroMan\LockTemplate.xlsm"
Dim tempLocation As String = Environ("USERPROFILE") & "\LockTemplate.xlsm"
...
xlWBTemp = xlApp.Workbooks.Open(tempLocation) '<~~ error here.

The error I get is:

System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
HResult=-2146827284
Message=Exception from HRESULT: 0x800A03EC

I've checked and the file definitely gets created, Excel is running and has already successfully opened a workbook at this point. I can't figure this out at all so any pointers truly welcome.


Update:

The file (tempLocation) does exist in the correct location, but when I open it outside of the application, Excel gives me the "found unreadable content" error. The file does still open successfully and has no problems once opened. I can now get around this error by using the CorruptLoad argument in the Workbooks.Open() method but I feel this is blindly ignoring the issue.


回答1:


If it helps here is an example of a simple VB.NET app that I wrote a long time ago to test speed of VB vs C#. It is a different way of going about creating a workbook and writing to it.

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.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")

        Dim l As Long
        l = 1
        Do While l < 500
            'xlWorkSheet.Cells(l, 1) = l
            xlWorkSheet.Range("A" & l).Value = l
            'xlWorkSheet.Cells(l, 2) = l
            'xlWorkSheet.Cells(l, 3) = l
            l = l + 1
        Loop

        xlWorkBook.Application.ScreenUpdating = True
        xlWorkBook.RefreshAll()
        xlWorkBook.Save()

        'xlWorkBook.Close()
        'xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Done")
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class


来源:https://stackoverflow.com/questions/33264298/winforms-com-exception-opening-excel-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!