I am having real problems trying to get a report creation process in my database to work correctly. The process is very complex. Basically what it does is export a number of reports to Excel files, and as it does this it adds them to a template Excel file which has a Main Menu linking to each of the reports. It then deletes the original report files, and saves the template to a relevant location. This seems to work correctly when run once, but if I try running multiple report batches without first exiting the database, it then does not add them to the template file correctly. So I assume it is because the Excel object is not shutting down correctly, so when I re-run the process it fails.
The code I have when first opening the Excel template invisibly is as follows:
'Start a new excel application in the background
Set oxlApp = New Excel.Application
'Turn off Excel warnings
oxlApp.Application.DisplayAlerts = False
'Open the report template spreadsheet
Set oxlTemplateBook = oxlApp.Workbooks.Open("C:\GroupedReportTemplate.xls")
Once I have finished adding the reports to the invisible template file, the finishing code which is designed to add finishing touches to the template, save it and then completely close the Excel objects is as follows:
'Open Main Menu sheet of the template
Set oxlWorksheet = oxlTemplateBook.Sheets("Main Menu")
'Disable gridlines and tabs
oxlApp.ActiveWindow.DisplayGridlines = False
oxlApp.ActiveWindow.DisplayHeadings = False
oxlApp.ActiveWindow.DisplayWorkbookTabs = False
'Save new reports spreadsheet to relevant directory and filename
oxlTemplateBook.SaveAs FileName:=strMultiDir & gstrSchedule & " Reports " & Format(Date, "ddmmyy") & ".xls"
'Close the template workbook
oxlTemplateBook.Close False
'Turn on Excel warnings
oxlApp.Application.DisplayAlerts = True
'Quit the Excel application
oxlApp.Quit
'Clear vars
Set oxlWorksheet = Nothing
Set oxlTemplateBook = Nothing
Set oxlApp = Nothing
I know that the Excel objects do not completely close, as if I try opening the created template file without first exiting the database, it is hidden and cannot be displayed correctly. Does anyone know how to get the objects to close completely so the process can be run multiple times without causing errors?
The code I have when first opening the Excel template invisibly is as follows:
'Start a new excel application in the background
Set oxlApp = New Excel.Application
'Turn off Excel warnings
oxlApp.Application.DisplayAlerts = False
'Open the report template spreadsheet
Set oxlTemplateBook = oxlApp.Workbooks.Open("C:\GroupedReportTemplate.xls")
Once I have finished adding the reports to the invisible template file, the finishing code which is designed to add finishing touches to the template, save it and then completely close the Excel objects is as follows:
'Open Main Menu sheet of the template
Set oxlWorksheet = oxlTemplateBook.Sheets("Main Menu")
'Disable gridlines and tabs
oxlApp.ActiveWindow.DisplayGridlines = False
oxlApp.ActiveWindow.DisplayHeadings = False
oxlApp.ActiveWindow.DisplayWorkbookTabs = False
'Save new reports spreadsheet to relevant directory and filename
oxlTemplateBook.SaveAs FileName:=strMultiDir & gstrSchedule & " Reports " & Format(Date, "ddmmyy") & ".xls"
'Close the template workbook
oxlTemplateBook.Close False
'Turn on Excel warnings
oxlApp.Application.DisplayAlerts = True
'Quit the Excel application
oxlApp.Quit
'Clear vars
Set oxlWorksheet = Nothing
Set oxlTemplateBook = Nothing
Set oxlApp = Nothing
I know that the Excel objects do not completely close, as if I try opening the created template file without first exiting the database, it is hidden and cannot be displayed correctly. Does anyone know how to get the objects to close completely so the process can be run multiple times without causing errors?