Rich P wrote:
Going along with Steve's line of reasoning I thought I would share how I
set up Excel objects from Access (since this is a significant portion of
my daily routine). I declare an Excel object variable (with a reference
to the respective Excel Object Library, Excel 8.0, 9.0, 10...)
Dim xlObj As Excel.Applicati on, wkbk As Excel.WorkBook
Dim sht As Excel.WorkSheet , rng As Excel.Range
..
Set xlObj = CreateObject("E xcel.Applicaion ")
Steve is correct about the delicateness of the New Keyword in VBA. I
didn't think about that because I have been steering away from using New
in VBA for Office Applications (although New works quite well in a
managed environment like VB.Net or C# - where the compiler will slap
your hand right away if you declare something incorrectly), but I
reserve using New for instantiating custom objects within Access
(objects created with a class module) because here you don't have to
worry about running an external process. It is internal to Access.
Rich
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Thanks very much for your input.
I actually only open one workbook per execution of this procedure and as
such I only need to call Quit once at the end of the process. I have
followed your advice above and unfortunately this has not woked for me.
Also, in your previous post your advised that I check Task Manager for
ALL possible instances of Excel running. Since including your above
recommendation I have found there to NO instances of Excel post code
execution.
I'm quite confused :)
Here is all of the code I'm using. Please excuse it, as its not very
modular at all. Its just something I've hacked up for free for brownie
points with for client. Would you mind checking it to see if there is
any glaring issue? I'd really apperciate it.
Option Compare Database
Option Explicit
Function ExportAmenityRa tingReport(strS QL As String) As Boolean
On Error GoTo eh_ExportAmenit yRatingReport
Dim dbLocal As Database
Dim rst As Recordset
Dim wbknew As Excel.Workbook, wksnew As Excel.Worksheet
Dim appExcel As Excel.Applicati on
Dim strCurrConditio n As String
Dim strBuilding As String
Dim intRow As Integer
Dim intStartRow As Integer
Dim intElementCount As Integer
Dim intRatingElemen tCount
Dim intTotalElement Count As Integer
Dim intBuildingCoun t As Integer
Dim strDestination As String
ExportAmenityRa tingReport = True
strDestination = API_FileSave("M S Excel Spreadsheet", "*.xls",
"C:\", "Save As...")
If strDestination = "C:\" Then Exit Function
If Right(strDestin ation, 4) <> ".xls" Then strDestination =
strDestination & ".xls"
If strSQL <> "" Then
strSQL = "SELECT * FROM qryBuildingAmen ityElementRatin gs WHERE
" & strSQL
Else
strSQL = "qryBuildingAme nityElementRati ngs"
End If
'open data table
Set dbLocal = CurrentDb()
Set rst = dbLocal.OpenRec ordset(strSQL, dbOpenSnapshot)
'set up Workbook
Set appExcel = CreateObject("E xcel.Applicatio n")
Set wbknew = appExcel.Workbo oks.Add
Set wksnew = wbknew.Workshee ts.Add
wksnew.Name = "Raw Data"
appExcel.Visibl e = True
WriteRawData wksnew, rst
Set wksnew = Nothing
Set wksnew = wbknew.Workshee ts.Add
wksnew.Name = "Rating Summary"
appExcel.Visibl e = True
WriteAmenityEle mentSummary wksnew
Set wksnew = Nothing
Set wksnew = wbknew.Workshee ts.Add
wksnew.Name = "Amenity Ratings"
appExcel.Visibl e = True
WriteHeader wksnew
rst.MoveFirst
intRow = 4
intTotalElement Count = 0
Do While Not rst.EOF
wksnew.Range(Ce lls(intRow, 1), Cells(intRow, 14)).select
Call SetEdgeBottomBo rder
intRow = intRow + 1
wksnew.Cells(in tRow, 2) = "Amenity Elements With Condition
Rating: " & rst!BuildingAme nityElementCond ition
wksnew.Cells(in tRow, 2).select
Call SetFont(True, True, 14)
strCurrConditio n = rst!BuildingAme nityElementCond ition
intBuildingCoun t = 0
intRatingElemen tCount = 0
Do While rst!BuildingAme nityElementCond ition = strCurrConditio n
intRow = intRow + 2
wksnew.Cells(in tRow, 3) = "Building Name: " & rst![Building
Name] & " Category: " & rst![Building Category]
wksnew.Cells(in tRow, 3).select
Call SetFont(True, False, 12)
intRow = intRow + 1
wksnew.Cells(in tRow, 4) = "Amenity Element Category:"
wksnew.Cells(in tRow, 7) = "Amenity Element Sub-Category:"
wksnew.Cells(in tRow, 11) = "Est Life:"
wksnew.Range(Ce lls(intRow, 2), Cells(intRow, 11)).select
Call SetFont(True, False, 9)
strBuilding = rst![Building Name]
intBuildingCoun t = intBuildingCoun t + 1
intElementCount = 0
Do While rst![Building Name] = strBuilding
intRow = intRow + 1
wksnew.Cells(in tRow, 4) = rst!AmenityElem entCategory
wksnew.Cells(in tRow, 7) = rst!AmenityElem entCategorySub
wksnew.Cells(in tRow, 11) =
rst!BuildingAme nityElementEstL ife
wksnew.Range(Ce lls(intRow, 4), Cells(intRow, 11)).select
Call SetFont(False, False, 9)
wksnew.Cells(in tRow, 11).select
Selection.Horiz ontalAlignment = xlCenter
intElementCount = intElementCount + 1
intRatingElemen tCount = intRatingElemen tCount + 1
rst.MoveNext
If rst.EOF Then Exit Do
Loop
intRow = intRow + 1
wksnew.Cells(in tRow, 3) = "Element Total: " &
intElementCount & " elements rated."
wksnew.Cells(in tRow, 3).select
Call SetFont(True, False, 12)
wksnew.Range(Ce lls(intRow, 3), Cells(intRow, 7)).select
Call SetEdgeTopBorde r
Call SetEdgeBottomBo rder
intRow = intRow + 2
If rst.EOF Then Exit Do
Loop
intTotalElement Count = intTotalElement Count + intRatingElemen tCount
wksnew.Cells(in tRow, 2) = "Rating Total: " & intElementCount &
" elements rated."
wksnew.Cells(in tRow, 2).select
Call SetFont(True, False, 9)
wksnew.Range(Ce lls(intRow, 1), Cells(intRow, 14)).select
Call SetEdgeTopBorde r
Call SetEdgeBottomBo rder
intRow = intRow + 2
If rst.EOF Then Exit Do
Loop
intRow = intRow + 1
wksnew.Range(Ce lls(intRow, 1), Cells(intRow, 14)).select
Call SetEdgeTopBorde r
Call SetEdgeBottomBo rder
intRow = intRow + 2
wksnew.Cells(in tRow, 2) = "Report Total: " & intBuildingCoun t & "
Buildings included in this report with " & intTotalElement Count & "
amenity elements rated."
wksnew.Cells(in tRow, 2).select
Call SetFont(True, False, 9)
wksnew.Cells(1, 1).select
With ActiveWindow
.Zoom = 100
.DisplayGridlin es = False
.DisplayHeading s = False
End With
wksnew.PageSetu p.Orientation = xlLandscape
Set wbknew = Nothing
Set wksnew = Nothing
ActiveWorkbook. SaveAs Filename:=strDe stination, _
FileFormat:=xlN ormal, Password:="", WriteResPasswor d:="",
ReadOnlyRecomme nded:=False, CreateBackup:=F alse
ActiveWorkbook. Close
appExcel.Quit
Set appExcel = Nothing
rst.Close
dbLocal.Close
Exit Function
eh_ExportAmenit yRatingReport:
ExportAmenityRa tingReport = False
End Function
Function ExportCondition ReportData() As Boolean
On Error Resume Next
Dim dbLocal As Database
Dim rst As Recordset
Dim wbknew As Excel.Workbook, wksnew As Excel.Worksheet
Dim appExcel As New Excel.Applicati on
Dim intRow As Integer
Dim i As Integer
Dim strDestination As String
Dim fld As Field
ExportCondition ReportData = True
strDestination = API_FileSave("M S Excel Spreadsheet", "*.xls",
"C:\", "Save As...")
If strDestination = "C:\" Then Exit Function
If Right(strDestin ation, 4) <> ".xls" Then strDestination =
strDestination & ".xls"
DoCmd.SetWarnin gs False
DoCmd.RunSQL "DELETE * FROM tempConditionEx port"
DoCmd.OpenQuery "qryConditionEx port"
'open data table
Set dbLocal = CurrentDb()
Set rst = dbLocal.OpenRec ordset("tempCon ditionExport", dbOpenSnapshot)
MsgBox "Depending on the number of records being written to Excel
this may take a few minutes.", vbInformation, "Informatio n."
'set up Workbook
Set wbknew = appExcel.Workbo oks.Add
Set wksnew = wbknew.Workshee ts.Add
wksnew.Name = "Condition Report Raw Data"
appExcel.Visibl e = True
rst.MoveFirst
intRow = 1
i = 1
For Each fld In rst.Fields
wksnew.Cells(in tRow, i) = fld.Name
i = i + 1
Next fld
intRow = 2
With rst
.MoveFirst
Do While Not .EOF
i = 1
For Each fld In rst.Fields
wksnew.Cells(in tRow, i) = fld.Value
i = i + 1
Next fld
.MoveNext
intRow = intRow + 1
Loop
End With
Set wbknew = Nothing
Set wksnew = Nothing
ActiveWorkbook. SaveAs Filename:=strDe stination, _
FileFormat:=xlN ormal, Password:="", WriteResPasswor d:="",
ReadOnlyRecomme nded:=False, CreateBackup:=F alse
'ActiveWorkbook .Close
'appExcel.Quit
Set appExcel = Nothing
Set wbknew = Nothing
Set wksnew = Nothing
rst.Close
dbLocal.Close
Exit Function
eh_ExportCondit ionReportData:
ExportCondition ReportData = False
End Function
Sub SetFont(booBold As Boolean, booItal As Boolean, intSize As Integer,
Optional strFont As String)
With Selection.Font
.Bold = booBold
.Italic = booItal
.Name = IIf(IsMissing(s trFont) Or strFont = "", "Times New
Roman", strFont)
.Size = intSize
End With
End Sub
Sub SetInteriorBord er()
With Selection.Borde rs(xlInsideVert ical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeLeftBord er()
With Selection.Borde rs(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeRightBor der()
With Selection.Borde rs(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeTopBorde r()
With Selection.Borde rs(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeBottomBo rder()
With Selection.Borde rs(xlEdgeBottom )
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeLeftBord erHeavy()
With Selection.Borde rs(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeRightBor derHeavy()
With Selection.Borde rs(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeTopBorde rHeavy()
With Selection.Borde rs(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeBottomBo rderHeavy()
With Selection.Borde rs(xlEdgeBottom )
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetSurroundHeav y()
Call SetEdgeLeftBord erHeavy
Call SetEdgeRightBor derHeavy
Call SetEdgeTopBorde rHeavy
Call SetEdgeBottomBo rderHeavy
End Sub
Sub SetSurround()
Call SetEdgeLeftBord er
Call SetEdgeRightBor der
Call SetEdgeTopBorde r
Call SetEdgeBottomBo rder
End Sub
Function WriteHeader(wks new As Worksheet) As Integer
Columns(1).Colu mnWidth = 1
Rows(1).RowHeig ht = 7.57
'creates the header for each Channel
wksnew.Cells(2, 2) = "Building Amenity Element Conditions (By Rating)"
wksnew.Cells(2, 2).select
Call SetFont(True, False, 24)
wksnew.Range(Ce lls(1, 1), Cells(1, 14)).select
Call SetEdgeBottomBo rder
End Function
Sub WriteFooter(int Row As Integer, wksnew As Worksheet, intStartRow As
Integer)
'writes and formats the subtotals for each Channel
intRow = intRow + 1
wksnew.Cells(in tRow, 5) = "Sub Total"
wksnew.Cells(in tRow, 5).select
Call SetFont(True, False, 10)
wksnew.Cells(in tRow, 6).Value = "=Sum(F" & intStartRow & ":F" &
intRow - 2 & ")"
wksnew.Cells(in tRow, 5).select
Call SetFont(True, False, 10)
Selection.Numbe rFormat = "$##,##0"
End Sub
Sub SetColumnWidths ()
Columns("A:A"). ColumnWidth = 7.57
Columns("B:B"). ColumnWidth = 4.29
Columns("C:C"). ColumnWidth = 7.43
Columns("D:D"). ColumnWidth = 18
Columns("E:E"). ColumnWidth = 26.86
Columns("F:F"). ColumnWidth = 12.71
Columns("G:G"). ColumnWidth = 19.57
Columns("H:H"). ColumnWidth = 10.86
Columns("I:I"). ColumnWidth = 11.14
Columns("J:J"). ColumnWidth = 18.57
Columns("A:J"). select
Selection.Horiz ontalAlignment = xlCenter
End Sub
Function WriteRawData(Wk s As Worksheet, rst As Recordset)
Dim intRow As Integer
intRow = 1
Wks.Cells(intRo w, 1) = "Condition Rating"
Wks.Cells(intRo w, 2) = "Building Name"
Wks.Cells(intRo w, 3) = "Amenity Element Category"
Wks.Cells(intRo w, 4) = "Amenity Element Category Sub"
Wks.Cells(intRo w, 5) = "Est. Life"
intRow = 2
With rst
.MoveFirst
Do While Not .EOF
Wks.Cells(intRo w, 1) = !BuildingAmenit yElementConditi on
Wks.Cells(intRo w, 2) = ![Building Name]
Wks.Cells(intRo w, 3) = !AmenityElement Category
Wks.Cells(intRo w, 4) = !AmenityElement CategorySub
Wks.Cells(intRo w, 5) = !BuildingAmenit yElementEstLife
.MoveNext
intRow = intRow + 1
Loop
End With
End Function
Function WriteAmenityEle mentSummary(Wks As Worksheet)
Dim intRow As Integer
Dim Db As Database
Dim rst As Recordset
Dim intNextSet As Integer
Columns(1).Colu mnWidth = 1
Rows(1).RowHeig ht = 7.57
intRow = 2
Wks.Cells(intRo w, 2) = "Rating Summary for Amenity Elements"
Wks.Cells(intRo w, 2).select
Call SetFont(True, False, 14)
intRow = 4
Wks.Cells(intRo w, 2) = "Amenity Element Category"
Wks.Cells(intRo w, 3) = "Rating 1"
Wks.Cells(intRo w, 4) = "Rating 2"
Wks.Cells(intRo w, 5) = "Rating 3"
Wks.Cells(intRo w, 6) = "Rating 4"
Wks.Cells(intRo w, 7) = "Rating 5"
Wks.Cells(intRo w, 8) = "Rating 6"
Wks.Range(Cells (intRow, 2), Cells(intRow, 8)).select
Call SetFont(True, False, 9)
Call SetSurroundHeav y
Set Db = CurrentDb()
Set rst = Db.OpenRecordse t("qryAmenityEl ementRatingSumm ary",
dbOpenSnapshot)
intRow = intRow + 1
With rst
.MoveFirst
Do While Not .EOF
Wks.Cells(intRo w, 2) = !AmenityElement Category
Wks.Cells(intRo w, 3) = !SumOfRating1
Wks.Cells(intRo w, 4) = !SumOfRating2
Wks.Cells(intRo w, 5) = !SumOfRating3
Wks.Cells(intRo w, 6) = !SumOfRating4
Wks.Cells(intRo w, 7) = !SumOfRating5
Wks.Cells(intRo w, 8) = !SumOfRating0
.MoveNext
intRow = intRow + 1
Loop
.Close
End With
Wks.Range(Cells (4, 2), Cells(intRow - 1, 2)).select
Call SetFont(True, False, 9)
Call SetSurroundHeav y
Wks.Range(Cells (4, 2), Cells(intRow - 1, 8)).select
Call SetSurroundHeav y
intRow = intRow + 3
Wks.Cells(intRo w, 2) = "Rating Summary for Sub Elements"
Wks.Cells(intRo w, 2).select
Call SetFont(True, False, 14)
intRow = intRow + 2
intNextSet = intRow
Wks.Cells(intRo w, 2) = "Amenity Element Category"
Wks.Cells(intRo w, 3) = "Amenity Element Sub-Category"
Wks.Cells(intRo w, 4) = "Rating 1"
Wks.Cells(intRo w, 5) = "Rating 2"
Wks.Cells(intRo w, 6) = "Rating 3"
Wks.Cells(intRo w, 7) = "Rating 4"
Wks.Cells(intRo w, 8) = "Rating 5"
Wks.Cells(intRo w, 9) = "Rating 6"
Wks.Range(Cells (intRow, 2), Cells(intRow, 9)).select
Call SetFont(True, False, 9)
Call SetSurroundHeav y
Set Db = CurrentDb()
Set rst = Db.OpenRecordse t("qryAmenitySu bElementRatingS ummary",
dbOpenSnapshot)
intRow = intRow + 1
With rst
.MoveFirst
Do While Not .EOF
Wks.Cells(intRo w, 2) = !AmenityElement Category
Wks.Cells(intRo w, 3) = !AmenityElement CategorySub
Wks.Cells(intRo w, 4) = !Rating1
Wks.Cells(intRo w, 5) = !Rating2
Wks.Cells(intRo w, 6) = !Rating3
Wks.Cells(intRo w, 7) = !Rating4
Wks.Cells(intRo w, 8) = !Rating5
Wks.Cells(intRo w, 9) = !Rating0
.MoveNext
intRow = intRow + 1
Loop
.Close
End With
intRow = intRow - 1
Wks.Range(Cells (intNextSet, 2), Cells(intRow, 2)).select
Call SetFont(True, False, 9)
Call SetSurroundHeav y
Wks.Range(Cells (intNextSet, 3), Cells(intRow, 3)).select
Call SetFont(True, False, 9)
Call SetSurroundHeav y
Wks.Range(Cells (intNextSet, 2), Cells(intRow, 9)).select
Call SetSurroundHeav y
Wks.Cells(1, 1).select
End Function