各位大佬好,我剛從vba轉來vb.net,還是云山霧罩的。
想實作的功能是,點擊一個按鈕,可以把檔案A中的被選中的幾個作業表合并插入到當前檔案的sheet1中。但運行時出錯:Exception thrown: 'System.Runtime.InteropServices.COMException' in ExcelTemplate5.dll
HResult=0x800A03EC Message=Exception from HRESULT: 0x800A03EC
Source=<Cannot evaluate the exception source>
StackTrace: <Cannot evaluate the exception stack trace>

其中ExcelTemplate5是專案名稱,這是個VSTO EXCEL 模板專案,不是excel插件。
按鈕的代碼:
Public Class FrmMain
Private Sub BtnImportIO_Click(sender As Object, e As EventArgs) Handles BtnImportIO.Click
If LstbxIO.SelectedIndex = -1 Then
MsgBox("No item is selected!")
Else
xlApp.ScreenUpdating = False
thisWbk = xlApp.ThisWorkbook ‘這一步出錯了
thisWst = thisWbk.Worksheets("IO_List")
xlApp = New Excel.Application With {.Visible = False}
Dim wbkFile As Excel.Workbook = xlApp.Workbooks.Open(strFilePath)
intTLastRow = 1
’For 回圈把選中的檔案A中的幾個作業表合并插入到當前檔案的sheet1中。因為前面就出錯了,這里就不貼出來了。
wbkFile.Close(SaveChanges:=0)
wbkFile = Nothing
LstbxValidation.Items.Add("Import completed.")
Application.DoEvents()
xlApp.ScreenUpdating = True
end if
End Sub
End Class
xlApp 在Module1中定義了啊,但不明白為什么不好用。
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Tools.Excel
Public Module Module1
Public strFilePath As String 'Store the full path of the source file (the file to be imported)
Public xlApp As Excel.Application = Globals.ThisWorkbook.Application
Public thisWbk As Excel.Workbook
Public thisWst As Excel.Worksheet
End Module
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/232800.html
標籤:VB.NET
下一篇:部署基于.netcore5.0的ABP框架后臺Api服務端,以及使用Nginx部署Vue+Element前端應用
