在 VB .NET 中添加新 Excel.application 时出错

Error when adding New Excel.application in VB .NET

对于一个项目,我必须能够从 Excel sheet 读取信息,然后将该信息传递给 PLC。 遗憾的是我在使用 'Dim App As New Excel.Application' 时遇到了问题 出于某种原因,这会使我的应用程序在表单显示在屏幕上之前崩溃,因此我最终没有表单。 我得到的错误是:

'System.InvalidOperationException' 类型的未处理异常发生在 OpHoopVanZegen.exe

这发生在 'Application.Designer.vb'

Protected Overrides Sub OnCreateMainForm()

            Me.MainForm = Global.OpHoopVanZegen.Form1
        End Sub

^^^哪里出错了^^^

附加信息: 无法将 COM 对象从类型 System.__ComObject 转换为接口类型

任何人都可以向我解释发生了什么和/或我做错了什么?

------------------------------------ 我的表单代码:

 Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Dim App As New Excel.Application
    Dim svrOPC As New OPCAutomation.OPCServer
    Dim WithEvents grpOPC As OPCAutomation.OPCGroup

Private Sub connectToOPC_Click(sender As Object, e As EventArgs) Handles connectToOPC.Click
    If svrOPC.ServerState = OPCAutomation.OPCServerState.OPCDisconnected Then
        Call svrOPC.Connect("Kepware.KEPServerEX.V5")
    End If
    Me.TBStatusOPC.Text = udfOPCServerState(svrOPC.ServerState)
End Sub
Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
    Me.TBStatusOPC.Text = udfOPCServerState(svrOPC.ServerState)
End Sub
Private Function udfOPCServerState(ByVal intOPCServerState As Integer) As String
    Select Case intOPCServerState
        Case OPCAutomation.OPCServerState.OPCRunning
            udfOPCServerState = "Running"
        Case OPCAutomation.OPCServerState.OPCFailed
            udfOPCServerState = "Failed"
        Case OPCAutomation.OPCServerState.OPCNoconfig
            udfOPCServerState = "No Config"
        Case OPCAutomation.OPCServerState.OPCSuspended
            udfOPCServerState = "Suspended"
        Case OPCAutomation.OPCServerState.OPCTest
            udfOPCServerState = "Test"
        Case OPCAutomation.OPCServerState.OPCDisconnected
            udfOPCServerState = "Disconnected"
        Case Else
            udfOPCServerState = "Unknown"
    End Select
End Function

Private Sub LoadOrderToPLC_Click(sender As Object, e As EventArgs) Handles LoadOrderToPLC.Click

End Sub

结束Class

我一直找不到很好的理由,但我在最近的一个项目中遇到了同样的问题。让事情对我有用的解决方法是:

Imports Microsoft.Office.Core
Dim app as Object
app = CreateObject("Excel.Application")

这使我的其余代码按我希望的方式工作,但缺点是 IntelliSense 对我的任何 Excel 变量都不起作用。由于我是一个懒鬼,非常依赖快捷方式而不是记住所有内容,所以我给自己写了一个注释掉的语法指南,并将其粘贴到我在代码中使用 spreadsheets 的任何地方以供参考。

希望对您有所帮助。我仍然很好奇是否有更好的方法来解决这个问题。

编辑:来自我自己代码的示例片段(包括我的作弊 sheet):

Imports Microsoft.Office.Core

Dim xl As Object, wb As Object, sht As Object, rng As Object, wrksht As Object

    'EXCEL SYNTAX GUIDE:
    'Excel.
    '     .Application  :   Instance of the Excel program opened by VB code
    '     .Workbooks    :   All open workbooks. Often use .Add or .Open when setting Workbook variable
    '     .Workbook     :   An individual workbook (.xls file). Often use .ActiveSheet to set Worksheet variable
    '     .Sheets       :   All worksheets (tabs) of a particular workbook
    '     .Worksheet    :   An individual worksheet
    '     .Range        :   A range of worksheet cells, e.g. ("A1", "C4")

    xl = CreateObject("Excel.Application")
    xl.Visible = False
    xl.DisplayAlerts = False
    wb = xl.Workbooks.Open(Environ("USERPROFILE") & "\dropbox\Program Database.xlsx")
    For Each wrksht In wb.Worksheets
        If wrksht.Name = "MASTER MATRIX" Then
            sht = wrksht
            Exit For
        End If
    Next
    For rNum = 6 To 201
        rng = sht.Range("A" & rNum, "AT" & rNum)
        Dim inputs(12) As String
        inputs(0) = rng.Cells(1, 1).Value
        inputs(1) = rng.Cells(1, 4).Value
        inputs(2) = rng.Cells(1, 5).Value
'This should be enough to demonstrate all the necessary syntax.
'
'
'Release variables and close Excel as follows:
'(Disabling alerts and user control prevents Excel from asking the user to save)
rng = Nothing
sht = Nothing
wb = Nothing
xl.DisplayAlerts = False
xl.UserControl = False
xl.Quit()
xl = Nothing