Results 1 to 1 of 1

Thread: [RESOLVED] Closing Excel from VB6

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2015
    Location
    Peterborough, UK
    Posts
    38

    Resolved [RESOLVED] Closing Excel from VB6

    I have an app that writes to an Excel workbook, recording dates, times, temperatures.
    I open Excel like this:
    Code:
    Private Sub Open_Excel()
        Set oXLApp = CreateObject("Excel.Application")
        If Err = 429 Then
            Set oXLApp = GetObject(, "Excel.Application")
        End If
        oXLApp.Workbooks.Open App.Path & "\Temp_Readings.xlsx"
        Set oXLBook = oXLApp.Workbooks.Item("Temp_Readings.xlsx")
    End Sub
    Then I close it like this:
    Code:
    Private Sub Close_Excel()
        On Error Resume Next
        oXLBook.Close savechanges:=True
        Set oXLSheet = Nothing
        Set oXLBook = Nothing
        If oXLApp <> "" Or oXLApp <> "Nothing" Then
            oXLApp.Quit
        End If
        Set oXLApp = Nothing
    End Sub
    This works, but seems awfully "heavy-handed" to me. Is there a better way?

    Also, if I'm running the code from the UI, and it gets stuck, for whatever reason, how can I tell Excel to close? I currently have to resort to Task Manager, which I could do without, if there's a better way for this, too?

    Thanks.

    Sorted it!

    This is now how I start Excel:
    Code:
    Private Sub Open_Excel()
        Set oXLBook = oXLApp.Workbooks.Open(App.Path & "\Temp_Readings.xlsx")  'Open an existing workbook
    End Sub
    And this is how I close:
    Code:
    Private Sub Close_Excel()
        oXLBook.Close SaveChanges:=True    'Save (and disconnect from) the Workbook
        Set oXLSheet = Nothing             'disconnect from the Worksheet
        Set oXLBook = Nothing
    End Sub
    Simple, when you know how!
    Last edited by JimWyseTech; Feb 15th, 2026 at 02:54 PM. Reason: Solved it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width