ProgrammingRookie
ProgrammingRookie

Reputation: 53

Error when adding New Excel.application in VB .NET

For a project I have to be able to read infromation from an Excel sheet and then pass that information on to a PLC. Sadly I have a problem when I use 'Dim App As New Excel.Application' For some reason this make my application crash before the form shows itself on the screen, so that I end up with no form. The Error I get is:

An unhandled exception of type 'System.InvalidOperationException' occurred in OpHoopVanZegen.exe

This happens in the 'Application.Designer.vb'

Protected Overrides Sub OnCreateMainForm()

            Me.MainForm = Global.OpHoopVanZegen.Form1
        End Sub

^^^Where the error happens^^^

Additional info: Cannot convert COM-object from type System.__ComObject to interfacetype

Anyone that could explain me what's going on and /or what I'm doing wrong?

------------------------------------ My Form Code:

 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

End Class

Upvotes: 1

Views: 5518

Answers (1)

Josh
Josh

Reputation: 1093

I never found a good reason why, but I had the same problem with a recent project. The workaround that got things functioning for me was:

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

This made the rest of my code work as I wanted it to, but with the drawback that IntelliSense didn't function for any of my Excel variables. Since I'm a slacker who relies heavily shortcuts rather than memorizing everything, I wrote myself a commented-out syntax guide and pasted it wherever I was working with spreadsheets in my code for reference.

I hope this helps. I'm still curious if there's a better way to fix this.

EDIT: An example snippet from my own code (with my cheat sheet included):

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

Upvotes: 1

Related Questions