Check if any excel file is open RRS feed

  • Question

  • I run the following code to check if any workbook is open. The exception occurs.

    How  can I fix it? Thanks!

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

        Dim xlApp As Excel.Application

           xlApp = New Excel.Application


    Thursday, August 22, 2019 7:54 AM


  • Hi,

    try my code:

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim oExcelApp As Microsoft.Office.Interop.Excel.Application
            oExcelApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
            For Each a In oExcelApp.Workbooks
                If a.Name.ToString().Substring(a.Name.Length - 5, 5).ToString = ".xlsx" Then
                End If
            oExcelApp = Nothing
        End Sub
    End Class

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    • Marked as answer by JayWangTPE Monday, August 26, 2019 9:14 AM
    Thursday, August 22, 2019 9:01 AM