.netexcelvb.netexceptiontypeinitializationexception

System.TypeInitializationException FORM access to Class


I'm working on a project where i need to call a class where i create and manage an excel file. But for undefined reasons i can't call this function from my FORM. It return an exception as this : System.TypeInitializationException. I've check my class but i don't see what is wrong.

The Form where i want to call my Excel_Management class :

Imports System.Diagnostics
Imports System.Windows.Forms

Public Class Proc_Form

Private excel_Manage = New Excel_Management
Public Shared ok As Boolean = New Boolean

 If My.Settings.check_Directory = False Then
            If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
                My.Settings.path = FolderBrowserDialog1.SelectedPath
                My.Settings.check_Directory = True
                'My.Settings.Save()
                MsgBox(My.Settings.path)
                excel_Manage.check_Excel()
        End If

The Excel_Management class :

Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Imports System.String

Public Class Excel_Management
    Public Shared appExcel As Excel.Application = New Excel.Application
    Public Shared wsExcel As Excel.Worksheet = New Excel.Worksheet
    Public Shared wbExcel As Excel.Workbook = New Excel.Workbook
    Public Shared file_name As String = "Name" & DateTime.Now.ToString("yyyy") & ".xls"
    Public Shared sheet_name As String = "S " & DatePart(DateInterval.WeekOfYear, Now())
    Public Shared check_test As Boolean = False
    Public Shared excel_check As New Proc_Form
    Public Shared misValue As Object = System.Reflection.Missing.Value
    Public Shared message, title, defaultValue As String
    Public Shared myValue As Object = New Object
    Public Shared i As Integer = 1
    Public Shared match As Match = Regex.Match(myValue, "[^a-z0-9]")

Shared Sub check_Excel()
        If appExcel Is Nothing Then
            MsgBox("No Excel Detected !")
            Return
        Else
            create_Excel()
        End If
    End Sub

Can you see any issue from my code that is not allowing me to access to my excel class?

Thank you in advance.


Solution

  • I think the issue is you can't create a New Excel.Workbook the way you are doing it since it's an interface.

    Try instead:

    Public Shared wbExcel As Excel.Workbook = appExcel.Workbooks.Add(System.Reflection.Missing.Value)
    

    From here: Strange error on Excel Workbook input, or one of the other solutions to that question.

    Also, your Regex.Match can't match on an object. I don't know what myValue is, but one option depending on what it is might be:

    Regex.Match(myValue.ToString, "[^a-z0-9]")
    

    Finally, these errors would be readily apparent to you if you get the debugger in Visual Studio to break on them. Go to "DEBUG" menu > "Exceptions..." and put a checkmark next to "Common Language Runtime Exceptions" in the "Thrown" column. That should help you debug any other errors.