M
Michele_L
I've read prior posts on getting rid of extra instances of Excel that using
the Excel application from within Access code can present. I've checked and
followed the objects created in the code, making sure to quit and set the
objects to nothing. Below is the code I've been working with, and it works
perfectly if I exit Access after it is run (which eliminates the extra
instance of Excel.) I'm using MS Access 2000, and would appreciate the help.
Dim db As DAO.Database
Dim rstlen As Integer
Dim rcs As DAO.Recordset
Dim erange As String
Dim myolapp As Outlook.Application
Dim myitem As MailItem
Dim att1 As Attachments
'DoCmd.RunMacro "M_Supplier Schedule"
Dim realcur As Variant
realcur = Now()
stnm = "U:\" & Format(realcur, "mm-dd-yyyy") & "-Supplier_Schedule.xls"
'*********************************************
'*********************************************
'*********************************************
DoCmd.OutputTo acOutputQuery, "Supplier Schedule", acFormatXLS, stnm, False
Dim appex As Object
Dim xsps As Object
Dim xlsheet As Object
Set appex = CreateObject("Excel.Application")
appex.Visible = True
appex.WindowState = xlMinimized
Set xsps = appex.Workbooks.Open(stnm)
Set xlsheet = xsps.ActiveSheet
xsps.Colors(38) = RGB(236, 239, 254)
xsps.Colors(55) = RGB(0, 0, 128)
xsps.Colors(56) = RGB(0, 0, 0)
Dim TNM As Variant
TNM = Format(realcur, "mmddyy")
xlsheet.Name = TNM & "-Supplier Schedule"
appex.Rows(1).Insert
xlsheet.Range("a1") = "Supplier Schedule As Of " & Format(realcur,
"mm-dd-yyyy")
xlsheet.Range("A2") = "SKU"
xlsheet.Range("b2") = "Division"
xlsheet.Range("c2") = "Subdivision"
xlsheet.Range("d2") = "Description"
xlsheet.Range("e2") = "Supplier #"
xlsheet.Range("f2") = "Supplier Name"
xlsheet.Range("g2") = "Supplier Addr1"
xlsheet.Range("h2") = "Supplier Addr2"
xlsheet.Range("i2") = "Supplier Addr3"
xlsheet.Range("j2") = "Supplier Addr4"
xlsheet.Range("k2") = "Supplier Addr5"
xlsheet.Range("l2") = "Zip"
xlsheet.Range("m2") = "Supplier #"
xlsheet.Range("n2") = "SKU"
xlsheet.Range("o2") = "Item Total"
xlsheet.Range("aa2") = "Family"
xlsheet.Range("ab2") = "Family Total"
xlsheet.Range("ac2") = "SKUs Per Family"
xlsheet.Range("ad2") = "Subdivision"
xlsheet.Range("ae2") = "Total Sub-Division Sum"
xlsheet.Range("af2") = "SKUs Per Sub-Division"
xlsheet.Range("ag2") = "Standard Cost"
xlsheet.Range("ah2") = "Current Price"
xlsheet.Range("ai2") = "Item Total Across"
Set db = CurrentDb()
Set rcs = db.OpenRecordset("Supplier Schedule")
rcs.MoveFirst
rcs.MoveLast
rstlen = rcs.RecordCount
rstlen = rstlen + 2
Set rcs = Nothing
Set db = Nothing
erange = "A3:ai" & rstlen
xlsheet.Range(erange).Select
With Selection
.Font.Name = "Arial"
.Font.Size = 8
.RowHeight = 12
.WrapText = True
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).ColorIndex = 56
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).ColorIndex = 56
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).ColorIndex = 56
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).ColorIndex = 56
.Borders(xlInsideVertical).LineStyle = xlContinuous
.Borders(xlInsideVertical).ColorIndex = 56
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Borders(xlInsideHorizontal).ColorIndex = 56
End With
xlsheet.Range("a2:AI2").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.Font.ColorIndex = 55
.Font.Size = 8
.Font.Bold = True
.RowHeight = 45.75
.AutoFilter
.Interior.ColorIndex = 38
End With
xlsheet.Range("A1").Select
With Selection
.Font.Bold = True
.RowHeight = 46
.Font.ColorIndex = 55
.WrapText = False
.Font.Size = 12
.RowHeight = 35
End With
xlsheet.Range("b1").RowHeight = 35
xlsheet.Range("b2").RowHeight = 50.25
xlsheet.Columns("A").ColumnWidth = 14.01
xlsheet.Columns("B").ColumnWidth = 7.57
xlsheet.Columns("C").ColumnWidth = 24.57
xlsheet.Columns("D").ColumnWidth = 35.14
xlsheet.Columns("E").ColumnWidth = 6.71
xlsheet.Columns("F").ColumnWidth = 32.57
xlsheet.Columns("G").ColumnWidth = 31.57
xlsheet.Columns("H").ColumnWidth = 32.29
xlsheet.Columns("I").ColumnWidth = 27.86
xlsheet.Columns("J").ColumnWidth = 26.43
xlsheet.Columns("K").ColumnWidth = 26.57
xlsheet.Columns("L").ColumnWidth = 5.43
xlsheet.Columns("M").ColumnWidth = 6.86
xlsheet.Columns("N").ColumnWidth = 12.86
xlsheet.Columns("O").ColumnWidth = 7.14
xlsheet.Columns("P:Z").ColumnWidth = 6.29
xlsheet.Columns("AA").ColumnWidth = 5.01
xlsheet.Columns("AB").ColumnWidth = 6.43
xlsheet.Columns("AC").ColumnWidth = 5.01
xlsheet.Columns("AD").ColumnWidth = 24.14
xlsheet.Columns("AE").ColumnWidth = 8.01
xlsheet.Columns("AF").ColumnWidth = 6.29
xlsheet.Columns("AG").ColumnWidth = 7.43
xlsheet.Columns("AH").ColumnWidth = 6.86
xlsheet.Columns("AI").ColumnWidth = 5.71
erange = "O3:Z" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0"
End With
erange = "AB3:AC" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0"
End With
erange = "AE3:AF" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0"
End With
erange = "AI3:AI" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0"
End With
erange = "AG3:AG" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0.000"
End With
erange = "AH3:AH" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0.000"
End With
With xlsheet.PageSetup
.PrintTitleRows = "$1:$2"
.PrintTitleColumns = "$A:$a"
End With
xlsheet.PageSetup.PrintArea = ""
With xlsheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page &P"
.RightFooter = ""
.LeftMargin = 0.0041
.RightMargin = 0.0041
.TopMargin = 0.0041
.BottomMargin = 0.36
.HeaderMargin = 0.0041
.FooterMargin = 0.0041
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 300
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLegal
.FirstPageNumber = xlAutomatic
.Order = xlOverThenDown
.BlackAndWhite = False
.Zoom = 80
End With
xlsheet.Range("A1").Select
xlsheet.Range("c3").Select
appex.ActiveWindow.FreezePanes = True
xlsheet.Range("A1").Select
appex.ActiveWindow.WindowState = xlMaximized
appex.DisplayAlerts = False
xsps.SaveAs stnm
xsps.Close
appex.Quit
Set xsps = Nothing
Set appex = Nothing
Set myolapp = CreateObject("outlook.application")
Set myitem = myolapp.createitem(MailItem)
Set att1 = myitem.Attachments
att1.Add stnm
myitem.To = "(e-mail address removed)"
myitem.Subject = "Supplier Schedule As Of " & Format(realcur, "mm-dd-yyyy")
myitem.send
Set myolapp = Nothing
Set myitem = Nothing
Set att1 = Nothing
MsgBox "Done ", vbOKOnly
the Excel application from within Access code can present. I've checked and
followed the objects created in the code, making sure to quit and set the
objects to nothing. Below is the code I've been working with, and it works
perfectly if I exit Access after it is run (which eliminates the extra
instance of Excel.) I'm using MS Access 2000, and would appreciate the help.
Dim db As DAO.Database
Dim rstlen As Integer
Dim rcs As DAO.Recordset
Dim erange As String
Dim myolapp As Outlook.Application
Dim myitem As MailItem
Dim att1 As Attachments
'DoCmd.RunMacro "M_Supplier Schedule"
Dim realcur As Variant
realcur = Now()
stnm = "U:\" & Format(realcur, "mm-dd-yyyy") & "-Supplier_Schedule.xls"
'*********************************************
'*********************************************
'*********************************************
DoCmd.OutputTo acOutputQuery, "Supplier Schedule", acFormatXLS, stnm, False
Dim appex As Object
Dim xsps As Object
Dim xlsheet As Object
Set appex = CreateObject("Excel.Application")
appex.Visible = True
appex.WindowState = xlMinimized
Set xsps = appex.Workbooks.Open(stnm)
Set xlsheet = xsps.ActiveSheet
xsps.Colors(38) = RGB(236, 239, 254)
xsps.Colors(55) = RGB(0, 0, 128)
xsps.Colors(56) = RGB(0, 0, 0)
Dim TNM As Variant
TNM = Format(realcur, "mmddyy")
xlsheet.Name = TNM & "-Supplier Schedule"
appex.Rows(1).Insert
xlsheet.Range("a1") = "Supplier Schedule As Of " & Format(realcur,
"mm-dd-yyyy")
xlsheet.Range("A2") = "SKU"
xlsheet.Range("b2") = "Division"
xlsheet.Range("c2") = "Subdivision"
xlsheet.Range("d2") = "Description"
xlsheet.Range("e2") = "Supplier #"
xlsheet.Range("f2") = "Supplier Name"
xlsheet.Range("g2") = "Supplier Addr1"
xlsheet.Range("h2") = "Supplier Addr2"
xlsheet.Range("i2") = "Supplier Addr3"
xlsheet.Range("j2") = "Supplier Addr4"
xlsheet.Range("k2") = "Supplier Addr5"
xlsheet.Range("l2") = "Zip"
xlsheet.Range("m2") = "Supplier #"
xlsheet.Range("n2") = "SKU"
xlsheet.Range("o2") = "Item Total"
xlsheet.Range("aa2") = "Family"
xlsheet.Range("ab2") = "Family Total"
xlsheet.Range("ac2") = "SKUs Per Family"
xlsheet.Range("ad2") = "Subdivision"
xlsheet.Range("ae2") = "Total Sub-Division Sum"
xlsheet.Range("af2") = "SKUs Per Sub-Division"
xlsheet.Range("ag2") = "Standard Cost"
xlsheet.Range("ah2") = "Current Price"
xlsheet.Range("ai2") = "Item Total Across"
Set db = CurrentDb()
Set rcs = db.OpenRecordset("Supplier Schedule")
rcs.MoveFirst
rcs.MoveLast
rstlen = rcs.RecordCount
rstlen = rstlen + 2
Set rcs = Nothing
Set db = Nothing
erange = "A3:ai" & rstlen
xlsheet.Range(erange).Select
With Selection
.Font.Name = "Arial"
.Font.Size = 8
.RowHeight = 12
.WrapText = True
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).ColorIndex = 56
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).ColorIndex = 56
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).ColorIndex = 56
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).ColorIndex = 56
.Borders(xlInsideVertical).LineStyle = xlContinuous
.Borders(xlInsideVertical).ColorIndex = 56
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Borders(xlInsideHorizontal).ColorIndex = 56
End With
xlsheet.Range("a2:AI2").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.Font.ColorIndex = 55
.Font.Size = 8
.Font.Bold = True
.RowHeight = 45.75
.AutoFilter
.Interior.ColorIndex = 38
End With
xlsheet.Range("A1").Select
With Selection
.Font.Bold = True
.RowHeight = 46
.Font.ColorIndex = 55
.WrapText = False
.Font.Size = 12
.RowHeight = 35
End With
xlsheet.Range("b1").RowHeight = 35
xlsheet.Range("b2").RowHeight = 50.25
xlsheet.Columns("A").ColumnWidth = 14.01
xlsheet.Columns("B").ColumnWidth = 7.57
xlsheet.Columns("C").ColumnWidth = 24.57
xlsheet.Columns("D").ColumnWidth = 35.14
xlsheet.Columns("E").ColumnWidth = 6.71
xlsheet.Columns("F").ColumnWidth = 32.57
xlsheet.Columns("G").ColumnWidth = 31.57
xlsheet.Columns("H").ColumnWidth = 32.29
xlsheet.Columns("I").ColumnWidth = 27.86
xlsheet.Columns("J").ColumnWidth = 26.43
xlsheet.Columns("K").ColumnWidth = 26.57
xlsheet.Columns("L").ColumnWidth = 5.43
xlsheet.Columns("M").ColumnWidth = 6.86
xlsheet.Columns("N").ColumnWidth = 12.86
xlsheet.Columns("O").ColumnWidth = 7.14
xlsheet.Columns("P:Z").ColumnWidth = 6.29
xlsheet.Columns("AA").ColumnWidth = 5.01
xlsheet.Columns("AB").ColumnWidth = 6.43
xlsheet.Columns("AC").ColumnWidth = 5.01
xlsheet.Columns("AD").ColumnWidth = 24.14
xlsheet.Columns("AE").ColumnWidth = 8.01
xlsheet.Columns("AF").ColumnWidth = 6.29
xlsheet.Columns("AG").ColumnWidth = 7.43
xlsheet.Columns("AH").ColumnWidth = 6.86
xlsheet.Columns("AI").ColumnWidth = 5.71
erange = "O3:Z" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0"
End With
erange = "AB3:AC" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0"
End With
erange = "AE3:AF" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0"
End With
erange = "AI3:AI" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0"
End With
erange = "AG3:AG" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0.000"
End With
erange = "AH3:AH" & rstlen
xlsheet.Range(erange).Select
With Selection
.NumberFormat = "#,##0.000"
End With
With xlsheet.PageSetup
.PrintTitleRows = "$1:$2"
.PrintTitleColumns = "$A:$a"
End With
xlsheet.PageSetup.PrintArea = ""
With xlsheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page &P"
.RightFooter = ""
.LeftMargin = 0.0041
.RightMargin = 0.0041
.TopMargin = 0.0041
.BottomMargin = 0.36
.HeaderMargin = 0.0041
.FooterMargin = 0.0041
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 300
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLegal
.FirstPageNumber = xlAutomatic
.Order = xlOverThenDown
.BlackAndWhite = False
.Zoom = 80
End With
xlsheet.Range("A1").Select
xlsheet.Range("c3").Select
appex.ActiveWindow.FreezePanes = True
xlsheet.Range("A1").Select
appex.ActiveWindow.WindowState = xlMaximized
appex.DisplayAlerts = False
xsps.SaveAs stnm
xsps.Close
appex.Quit
Set xsps = Nothing
Set appex = Nothing
Set myolapp = CreateObject("outlook.application")
Set myitem = myolapp.createitem(MailItem)
Set att1 = myitem.Attachments
att1.Add stnm
myitem.To = "(e-mail address removed)"
myitem.Subject = "Supplier Schedule As Of " & Format(realcur, "mm-dd-yyyy")
myitem.send
Set myolapp = Nothing
Set myitem = Nothing
Set att1 = Nothing
MsgBox "Done ", vbOKOnly