I am using MS Access VBA to create and excel spreadsheet and format the sheet from MS Access. When I run my code, it runs great and I get the formatted report exacly as I wanted. Then when I run it again, it throws an "Object Variable or With Block Variable not set" Error.
So my code runs from a button click on a form. Initially I do some stuff to create a suffix for the file name (in this format: YYYY_MM_DD_(AM/PM)HH). Then I create a file from a table in MS Access. If the file already exists, I prompt the user to overwrite it or not... I pretty much always overwrite it as I am trying to see if my formatting code works.
I am not sure if my reference to the report is off. I'm not really sure what's going on... but It works great one round... then I click the button again and it breaks, (I have to close the application when it breaks) then I click the button again and it works fine... rinse... repeat...
Here's the section of the code that seems to be the issue:
I've attached a screenshot of the part of the code where it actually halts. Like I said... I think it might be my referencing of the Excel object, but if it is... I don't know what I need to do to prevent this.
Last thing... I've copied the full sub into notepad and attached, if anyone needs to see the full version. It's pretty long so I didn't want to post it on the thread. I know the ridiculously long case statements can be consolidated, I haven't gotten to that part yet...
Thanks, in advance...
-Gary
So my code runs from a button click on a form. Initially I do some stuff to create a suffix for the file name (in this format: YYYY_MM_DD_(AM/PM)HH). Then I create a file from a table in MS Access. If the file already exists, I prompt the user to overwrite it or not... I pretty much always overwrite it as I am trying to see if my formatting code works.
I am not sure if my reference to the report is off. I'm not really sure what's going on... but It works great one round... then I click the button again and it breaks, (I have to close the application when it breaks) then I click the button again and it works fine... rinse... repeat...
Here's the section of the code that seems to be the issue:
Code:
strReportPath = "C:\Users\garland.black\Desktop\CONTROL_DESK_REPORT_" & strSUFFIX & ".xlsx"
'If this file exists prompt user to overwrite
If Dir(strReportPath) <> "" Then
vbAnswer = MsgBox("This File: [" & strReportPath & "] already Exists, do you wish to overwirte it?", vbYesNo)
If vbAnswer = vbYes Then
Kill strReportPath
Else
MsgBox ("No new data will be applied to: " & strReportPath)
Exit Sub
End If
Else
MsgBox "File doesn't exist."
End If
'Export Report
DoCmd.OutputTo acOutputTable, "001_CONTROL_DESK_REPORT", acFormatXLSX, strReportPath
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = True
appExcel.Application.Workbooks.Open (strReportPath)
Set objActiveWkb = appExcel.Application.ActiveWorkbook
'----------------------------------------------------------------------------
' Run all Report Formatting
'----------------------------------------------------------------------------
'Turns alerts off
appExcel.Application.DisplayAlerts = False
'------------------------------------------------------------------------------------------
' Gets the first and last row (last row should be the record containing Query Order "200")
'------------------------------------------------------------------------------------------
First_Row = 1
Last_Row = CLng(appExcel.Application.ActiveWorkbook.Worksheets("001_CONTROL_DESK_REPORT").UsedRange.Rows.Count)
'appExcel.Application.ActiveWorkbook.Worksheets("001_CONTROL_DESK_REPORT").Range("K" & CStr(First_Row) & ":K" & CStr(Last_Group_Row)).Select
'--------------------------------------------------------------------------------------------------
' SORT ON QUERY ORDER HERE:
'--------------------------------------------------------------------------------------------------
appExcel.Application.ActiveWorkbook.Worksheets("001_CONTROL_DESK_REPORT").Range("L2").Select
appExcel.Application.ActiveWorkbook.Worksheets("001_CONTROL_DESK_REPORT").Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("001_CONTROL_DESK_REPORT").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("001_CONTROL_DESK_REPORT").Sort.SortFields.Add Key _
:=Range("L2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("001_CONTROL_DESK_REPORT").Sort
.SetRange Range("A2:M" & CStr(Last_Row))
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I've attached a screenshot of the part of the code where it actually halts. Like I said... I think it might be my referencing of the Excel object, but if it is... I don't know what I need to do to prevent this.
Last thing... I've copied the full sub into notepad and attached, if anyone needs to see the full version. It's pretty long so I didn't want to post it on the thread. I know the ridiculously long case statements can be consolidated, I haven't gotten to that part yet...
Thanks, in advance...
-Gary