I have an app that writes to an Excel workbook, recording dates, times, temperatures.
I open Excel like this:
Then I close it 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
This works, but seems awfully "heavy-handed" to me. Is there a better way?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
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:
And this is how I close:Code:Private Sub Open_Excel() Set oXLBook = oXLApp.Workbooks.Open(App.Path & "\Temp_Readings.xlsx") 'Open an existing workbook End Sub
Simple, when you know how!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


Reply With Quote
