Hi
I'm running a piece of code that extracts a query to excel, formats & then mails it out.
The first time I run the report it works fine, but the second time i get the above error.
I have figured out why...excel is still running in the background & if i go to task manager and end the .exe then it will run again.
How can i get my code to do this automatically? The code i currently have is below...
Private Sub Command2_Click()
Dim objex As Excel.Application
Set objex = New Excel.Application
If Nz(WeekNumber, 0) = 0 Then
MsgBox "A valid week number is required"
Else
If Nz(Year, 0) = 0 Then
MsgBox "A valid year is required"
Else
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "qryRWAReport01", "H:\Service Centre\Reports\Wkly Reports\" & Year & "\wk" & " " & WeekNumber & "\RWAReportwk" & WeekNumber & ".xls", True
End If
End If
'format excel spreadsheet
objex.Workbooks.Open "H:\Service Centre\Reports\Wkly Reports\" & Year & "\wk" & " " & WeekNumber & "\RWAReportwk" & WeekNumber & ".xls"
'objex.Sheets("qryRWAReport01").Activate
'objex.Visible = True
'Workbooks.Application.ScreenUpdating = False
With Worksheets("qryRWAReport01").Cells.Font
.Name = "Arial"
.Size = 9
End With
Worksheets("qryRWAReport01").Columns.AutoFit
Range("A2").Select
ActiveWindow.FreezePanes = True
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page &P"
.RightFooter = ""
.LeftMargin = Workbooks.Application.InchesToPoints(0.75)
.RightMargin = Workbooks.Application.InchesToPoints(0.75)
.TopMargin = Workbooks.Application.InchesToPoints(1)
.BottomMargin = Workbooks.Application.InchesToPoints(1)
.HeaderMargin = Workbooks.Application.InchesToPoints(0.5)
.FooterMargin = Workbooks.Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = True
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Workbooks.Application.ScreenUpdating = True
Workbooks("RWAReportwk" & WeekNumber & ".xls").Save
Workbooks("RWAReportwk" & WeekNumber & ".xls").Close
objex.Quit
Set objex = Nothing
....then all my mailing code....
Any help would be great, i've looked at other posts but can't seem to figure out a fix...
I'm running a piece of code that extracts a query to excel, formats & then mails it out.
The first time I run the report it works fine, but the second time i get the above error.
I have figured out why...excel is still running in the background & if i go to task manager and end the .exe then it will run again.
How can i get my code to do this automatically? The code i currently have is below...
Private Sub Command2_Click()
Dim objex As Excel.Application
Set objex = New Excel.Application
If Nz(WeekNumber, 0) = 0 Then
MsgBox "A valid week number is required"
Else
If Nz(Year, 0) = 0 Then
MsgBox "A valid year is required"
Else
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "qryRWAReport01", "H:\Service Centre\Reports\Wkly Reports\" & Year & "\wk" & " " & WeekNumber & "\RWAReportwk" & WeekNumber & ".xls", True
End If
End If
'format excel spreadsheet
objex.Workbooks.Open "H:\Service Centre\Reports\Wkly Reports\" & Year & "\wk" & " " & WeekNumber & "\RWAReportwk" & WeekNumber & ".xls"
'objex.Sheets("qryRWAReport01").Activate
'objex.Visible = True
'Workbooks.Application.ScreenUpdating = False
With Worksheets("qryRWAReport01").Cells.Font
.Name = "Arial"
.Size = 9
End With
Worksheets("qryRWAReport01").Columns.AutoFit
Range("A2").Select
ActiveWindow.FreezePanes = True
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page &P"
.RightFooter = ""
.LeftMargin = Workbooks.Application.InchesToPoints(0.75)
.RightMargin = Workbooks.Application.InchesToPoints(0.75)
.TopMargin = Workbooks.Application.InchesToPoints(1)
.BottomMargin = Workbooks.Application.InchesToPoints(1)
.HeaderMargin = Workbooks.Application.InchesToPoints(0.5)
.FooterMargin = Workbooks.Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = True
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Workbooks.Application.ScreenUpdating = True
Workbooks("RWAReportwk" & WeekNumber & ".xls").Save
Workbooks("RWAReportwk" & WeekNumber & ".xls").Close
objex.Quit
Set objex = Nothing
....then all my mailing code....
Any help would be great, i've looked at other posts but can't seem to figure out a fix...