Hi all
I have some code which uses excel templates to insert query data in and email to customers. I can't seem to quite get the code right for cleaning the excel operation afterwards. If the user already had a copy of excel running, I want to close these new worksheets only but leave any other sheets open. It either closes all of excel, or seems to close the wrong spreadsheet. Please can someone help me with this? I'm including the code below for the lines which I think you need to see, I haven't included the complete code...
Thanks
' Establish an EXCEL application object
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
Else
blnEXCEL = False
End If
Err.Clear
On Error GoTo 0
Set db = CurrentDB()
If DCount("*", "Excel PosCode Report") <> 0 Then
Set rst = db.OpenRecordset("Excel PosCode Report")
xlx.Visible = False
Set xlw = xlx.Workbooks.Open("c:\Reports\Excel PosCode Report.xls")
Set xls = xlw.Worksheets("Excel PosCode Report")
rst.Close
Set rst = Nothing
Set qdf = Nothing
Set xlc = Nothing
Set xls = Nothing
'xlx.Quit
xlw.Close True ' close the EXCEL file and save the new data
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
db.Close
Set db = Nothing
I have some code which uses excel templates to insert query data in and email to customers. I can't seem to quite get the code right for cleaning the excel operation afterwards. If the user already had a copy of excel running, I want to close these new worksheets only but leave any other sheets open. It either closes all of excel, or seems to close the wrong spreadsheet. Please can someone help me with this? I'm including the code below for the lines which I think you need to see, I haven't included the complete code...
Thanks
' Establish an EXCEL application object
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
Else
blnEXCEL = False
End If
Err.Clear
On Error GoTo 0
Set db = CurrentDB()
If DCount("*", "Excel PosCode Report") <> 0 Then
Set rst = db.OpenRecordset("Excel PosCode Report")
xlx.Visible = False
Set xlw = xlx.Workbooks.Open("c:\Reports\Excel PosCode Report.xls")
Set xls = xlw.Worksheets("Excel PosCode Report")
rst.Close
Set rst = Nothing
Set qdf = Nothing
Set xlc = Nothing
Set xls = Nothing
'xlx.Quit
xlw.Close True ' close the EXCEL file and save the new data
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
db.Close
Set db = Nothing