שתף באמצעות


Close an open excel in VB.NET

Question

Tuesday, August 13, 2019 6:32 AM

I wrote the following sub to open an excel file in a sub and close it in another sub.

When I run CloseToolStripMenuItem code to close an open excel. One exception occurs.

How can I fix this issue?

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

   Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        'Dim selectfile As String
        WorkbookNameBox.Text = Getfilename("*.xlsm")
        selectfile = WorkbookNameBox.Text
        OpenExcelDemo(WorkbookNameBox.Text)
    End Sub
    Private Sub OpenExcelDemo(ByVal FileName As String)
        If IO.File.Exists(FileName) Then
            Dim Proceed As Boolean = False
            Dim xlApp As Excel.Application = Nothing
            Dim xlWorkBooks As Excel.Workbooks = Nothing
            Dim xlWorkBook As Excel.Workbook = Nothing
            ' Dim xlWorkSheet As Excel.Worksheet = Nothing
            ' Dim xlWorkSheets As Excel.Sheets = Nothing
            'Dim xlCells As Excel.Range = Nothing
            xlApp = New Excel.Application
            xlApp.DisplayAlerts = False
            xlWorkBooks = xlApp.Workbooks
            xlWorkBook = xlWorkBooks.Open(FileName)
            xlApp.Visible = True
            'xlWorkSheets = xlWorkBook.Sheets
        End If
    End Sub

    Private Sub CloseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseToolStripMenuItem.Click
        Dim xlApp As Excel.Application = Nothing
        Dim xlWorkBooks As Excel.Workbooks = Nothing
        Dim xlWorkBook As Excel.Workbook = Nothing
        xlWorkBook.Close()
        xlApp.UserControl = True
        xlApp.Quit()

    End Sub

All replies (3)

Tuesday, August 13, 2019 8:08 AM ✅Answered | 1 vote

Hi,

try my demo

Imports Microsoft.Office.Interop

Public Class Form1
    Dim xlApp As Excel.Application
    Dim xlWorkBooks As Excel.Workbooks
    Dim xlWorkBook As Excel.Workbook
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Dim selectfile As String
        'WorkbookNameBox.Text = Getfilename("*.xlsm")
        'selectfile = WorkbookNameBox.Text
        'OpenExcelDemo(WorkbookNameBox.Text)
        OpenExcelDemo("D:\test.xlsx")
    End Sub
    Private Sub OpenExcelDemo(ByVal FileName As String)
        If IO.File.Exists(FileName) Then
            Dim Proceed As Boolean = False
            xlApp = Nothing
            xlWorkBooks = Nothing
            xlWorkBook = Nothing
            ' Dim xlWorkSheet As Excel.Worksheet = Nothing
            ' Dim xlWorkSheets As Excel.Sheets = Nothing
            'Dim xlCells As Excel.Range = Nothing
            xlApp = New Excel.Application
            xlApp.DisplayAlerts = False
            xlWorkBooks = xlApp.Workbooks
            xlWorkBook = xlWorkBooks.Open(FileName)
            xlApp.Visible = True
            'xlWorkSheets = xlWorkBook.Sheets
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        xlWorkBook.Close()
        xlApp.UserControl = True
        xlApp.Quit()
    End Sub
End Class

Best Regards,

Alex

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 MSDNFSF@microsoft.com.


Tuesday, August 13, 2019 8:51 AM

Hi Alex,

It works!

Thanks,

Jay


Tuesday, August 13, 2019 8:53 AM

Hi,

If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

Best Regards,

Alex

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 MSDNFSF@microsoft.com.