Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

vb.net - WinForms COM Exception opening Excel workbook

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:UsersMacroManLockTemplate.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...