Rich P wrote:[color=blue]
> 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.Application, wkbk As Excel.WorkBook
> Dim sht As Excel.WorkSheet, rng As Excel.Range
> ..
> Set xlObj = CreateObject("Excel.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![/color]
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 ExportAmenityRatingReport(strSQL As String) As Boolean
On Error GoTo eh_ExportAmenityRatingReport
Dim dbLocal As Database
Dim rst As Recordset
Dim wbknew As Excel.Workbook, wksnew As Excel.Worksheet
Dim appExcel As Excel.Application
Dim strCurrCondition As String
Dim strBuilding As String
Dim intRow As Integer
Dim intStartRow As Integer
Dim intElementCount As Integer
Dim intRatingElementCount
Dim intTotalElementCount As Integer
Dim intBuildingCount As Integer
Dim strDestination As String
ExportAmenityRatingReport = True
strDestination = API_FileSave("MS Excel Spreadsheet", "*.xls",
"C:\", "Save As...")
If strDestination = "C:\" Then Exit Function
If Right(strDestination, 4) <> ".xls" Then strDestination =
strDestination & ".xls"
If strSQL <> "" Then
strSQL = "SELECT * FROM qryBuildingAmenityElementRatings WHERE
" & strSQL
Else
strSQL = "qryBuildingAmenityElementRatings"
End If
'open data table
Set dbLocal = CurrentDb()
Set rst = dbLocal.OpenRecordset(strSQL, dbOpenSnapshot)
'set up Workbook
Set appExcel = CreateObject("Excel.Application")
Set wbknew = appExcel.Workbooks.Add
Set wksnew = wbknew.Worksheets.Add
wksnew.Name = "Raw Data"
appExcel.Visible = True
WriteRawData wksnew, rst
Set wksnew = Nothing
Set wksnew = wbknew.Worksheets.Add
wksnew.Name = "Rating Summary"
appExcel.Visible = True
WriteAmenityElementSummary wksnew
Set wksnew = Nothing
Set wksnew = wbknew.Worksheets.Add
wksnew.Name = "Amenity Ratings"
appExcel.Visible = True
WriteHeader wksnew
rst.MoveFirst
intRow = 4
intTotalElementCount = 0
Do While Not rst.EOF
wksnew.Range(Cells(intRow, 1), Cells(intRow, 14)).select
Call SetEdgeBottomBorder
intRow = intRow + 1
wksnew.Cells(intRow, 2) = "Amenity Elements With Condition
Rating: " & rst!BuildingAmenityElementCondition
wksnew.Cells(intRow, 2).select
Call SetFont(True, True, 14)
strCurrCondition = rst!BuildingAmenityElementCondition
intBuildingCount = 0
intRatingElementCount = 0
Do While rst!BuildingAmenityElementCondition = strCurrCondition
intRow = intRow + 2
wksnew.Cells(intRow, 3) = "Building Name: " & rst![Building
Name] & " Category: " & rst![Building Category]
wksnew.Cells(intRow, 3).select
Call SetFont(True, False, 12)
intRow = intRow + 1
wksnew.Cells(intRow, 4) = "Amenity Element Category:"
wksnew.Cells(intRow, 7) = "Amenity Element Sub-Category:"
wksnew.Cells(intRow, 11) = "Est Life:"
wksnew.Range(Cells(intRow, 2), Cells(intRow, 11)).select
Call SetFont(True, False, 9)
strBuilding = rst![Building Name]
intBuildingCount = intBuildingCount + 1
intElementCount = 0
Do While rst![Building Name] = strBuilding
intRow = intRow + 1
wksnew.Cells(intRow, 4) = rst!AmenityElementCategory
wksnew.Cells(intRow, 7) = rst!AmenityElementCategorySub
wksnew.Cells(intRow, 11) =
rst!BuildingAmenityElementEstLife
wksnew.Range(Cells(intRow, 4), Cells(intRow, 11)).select
Call SetFont(False, False, 9)
wksnew.Cells(intRow, 11).select
Selection.HorizontalAlignment = xlCenter
intElementCount = intElementCount + 1
intRatingElementCount = intRatingElementCount + 1
rst.MoveNext
If rst.EOF Then Exit Do
Loop
intRow = intRow + 1
wksnew.Cells(intRow, 3) = "Element Total: " &
intElementCount & " elements rated."
wksnew.Cells(intRow, 3).select
Call SetFont(True, False, 12)
wksnew.Range(Cells(intRow, 3), Cells(intRow, 7)).select
Call SetEdgeTopBorder
Call SetEdgeBottomBorder
intRow = intRow + 2
If rst.EOF Then Exit Do
Loop
intTotalElementCount = intTotalElementCount + intRatingElementCount
wksnew.Cells(intRow, 2) = "Rating Total: " & intElementCount &
" elements rated."
wksnew.Cells(intRow, 2).select
Call SetFont(True, False, 9)
wksnew.Range(Cells(intRow, 1), Cells(intRow, 14)).select
Call SetEdgeTopBorder
Call SetEdgeBottomBorder
intRow = intRow + 2
If rst.EOF Then Exit Do
Loop
intRow = intRow + 1
wksnew.Range(Cells(intRow, 1), Cells(intRow, 14)).select
Call SetEdgeTopBorder
Call SetEdgeBottomBorder
intRow = intRow + 2
wksnew.Cells(intRow, 2) = "Report Total: " & intBuildingCount & "
Buildings included in this report with " & intTotalElementCount & "
amenity elements rated."
wksnew.Cells(intRow, 2).select
Call SetFont(True, False, 9)
wksnew.Cells(1, 1).select
With ActiveWindow
.Zoom = 100
.DisplayGridlines = False
.DisplayHeadings = False
End With
wksnew.PageSetup.Orientation = xlLandscape
Set wbknew = Nothing
Set wksnew = Nothing
ActiveWorkbook.SaveAs Filename:=strDestination, _
FileFormat:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close
appExcel.Quit
Set appExcel = Nothing
rst.Close
dbLocal.Close
Exit Function
eh_ExportAmenityRatingReport:
ExportAmenityRatingReport = False
End Function
Function ExportConditionReportData() 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.Application
Dim intRow As Integer
Dim i As Integer
Dim strDestination As String
Dim fld As Field
ExportConditionReportData = True
strDestination = API_FileSave("MS Excel Spreadsheet", "*.xls",
"C:\", "Save As...")
If strDestination = "C:\" Then Exit Function
If Right(strDestination, 4) <> ".xls" Then strDestination =
strDestination & ".xls"
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tempConditionExport"
DoCmd.OpenQuery "qryConditionExport"
'open data table
Set dbLocal = CurrentDb()
Set rst = dbLocal.OpenRecordset("tempConditionExport", dbOpenSnapshot)
MsgBox "Depending on the number of records being written to Excel
this may take a few minutes.", vbInformation, "Information."
'set up Workbook
Set wbknew = appExcel.Workbooks.Add
Set wksnew = wbknew.Worksheets.Add
wksnew.Name = "Condition Report Raw Data"
appExcel.Visible = True
rst.MoveFirst
intRow = 1
i = 1
For Each fld In rst.Fields
wksnew.Cells(intRow, 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(intRow, i) = fld.Value
i = i + 1
Next fld
.MoveNext
intRow = intRow + 1
Loop
End With
Set wbknew = Nothing
Set wksnew = Nothing
ActiveWorkbook.SaveAs Filename:=strDestination, _
FileFormat:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False, CreateBackup:=False
'ActiveWorkbook.Close
'appExcel.Quit
Set appExcel = Nothing
Set wbknew = Nothing
Set wksnew = Nothing
rst.Close
dbLocal.Close
Exit Function
eh_ExportConditionReportData:
ExportConditionReportData = 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(strFont) Or strFont = "", "Times New
Roman", strFont)
.Size = intSize
End With
End Sub
Sub SetInteriorBorder()
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeLeftBorder()
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeRightBorder()
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeTopBorder()
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeBottomBorder()
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeLeftBorderHeavy()
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeRightBorderHeavy()
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeTopBorderHeavy()
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetEdgeBottomBorderHeavy()
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
End Sub
Sub SetSurroundHeavy()
Call SetEdgeLeftBorderHeavy
Call SetEdgeRightBorderHeavy
Call SetEdgeTopBorderHeavy
Call SetEdgeBottomBorderHeavy
End Sub
Sub SetSurround()
Call SetEdgeLeftBorder
Call SetEdgeRightBorder
Call SetEdgeTopBorder
Call SetEdgeBottomBorder
End Sub
Function WriteHeader(wksnew As Worksheet) As Integer
Columns(1).ColumnWidth = 1
Rows(1).RowHeight = 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(Cells(1, 1), Cells(1, 14)).select
Call SetEdgeBottomBorder
End Function
Sub WriteFooter(intRow As Integer, wksnew As Worksheet, intStartRow As
Integer)
'writes and formats the subtotals for each Channel
intRow = intRow + 1
wksnew.Cells(intRow, 5) = "Sub Total"
wksnew.Cells(intRow, 5).select
Call SetFont(True, False, 10)
wksnew.Cells(intRow, 6).Value = "=Sum(F" & intStartRow & ":F" &
intRow - 2 & ")"
wksnew.Cells(intRow, 5).select
Call SetFont(True, False, 10)
Selection.NumberFormat = "$##,##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.HorizontalAlignment = xlCenter
End Sub
Function WriteRawData(Wks As Worksheet, rst As Recordset)
Dim intRow As Integer
intRow = 1
Wks.Cells(intRow, 1) = "Condition Rating"
Wks.Cells(intRow, 2) = "Building Name"
Wks.Cells(intRow, 3) = "Amenity Element Category"
Wks.Cells(intRow, 4) = "Amenity Element Category Sub"
Wks.Cells(intRow, 5) = "Est. Life"
intRow = 2
With rst
.MoveFirst
Do While Not .EOF
Wks.Cells(intRow, 1) = !BuildingAmenityElementCondition
Wks.Cells(intRow, 2) = ![Building Name]
Wks.Cells(intRow, 3) = !AmenityElementCategory
Wks.Cells(intRow, 4) = !AmenityElementCategorySub
Wks.Cells(intRow, 5) = !BuildingAmenityElementEstLife
.MoveNext
intRow = intRow + 1
Loop
End With
End Function
Function WriteAmenityElementSummary(Wks As Worksheet)
Dim intRow As Integer
Dim Db As Database
Dim rst As Recordset
Dim intNextSet As Integer
Columns(1).ColumnWidth = 1
Rows(1).RowHeight = 7.57
intRow = 2
Wks.Cells(intRow, 2) = "Rating Summary for Amenity Elements"
Wks.Cells(intRow, 2).select
Call SetFont(True, False, 14)
intRow = 4
Wks.Cells(intRow, 2) = "Amenity Element Category"
Wks.Cells(intRow, 3) = "Rating 1"
Wks.Cells(intRow, 4) = "Rating 2"
Wks.Cells(intRow, 5) = "Rating 3"
Wks.Cells(intRow, 6) = "Rating 4"
Wks.Cells(intRow, 7) = "Rating 5"
Wks.Cells(intRow, 8) = "Rating 6"
Wks.Range(Cells(intRow, 2), Cells(intRow, 8)).select
Call SetFont(True, False, 9)
Call SetSurroundHeavy
Set Db = CurrentDb()
Set rst = Db.OpenRecordset("qryAmenityElementRatingSummary",
dbOpenSnapshot)
intRow = intRow + 1
With rst
.MoveFirst
Do While Not .EOF
Wks.Cells(intRow, 2) = !AmenityElementCategory
Wks.Cells(intRow, 3) = !SumOfRating1
Wks.Cells(intRow, 4) = !SumOfRating2
Wks.Cells(intRow, 5) = !SumOfRating3
Wks.Cells(intRow, 6) = !SumOfRating4
Wks.Cells(intRow, 7) = !SumOfRating5
Wks.Cells(intRow, 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 SetSurroundHeavy
Wks.Range(Cells(4, 2), Cells(intRow - 1, 8)).select
Call SetSurroundHeavy
intRow = intRow + 3
Wks.Cells(intRow, 2) = "Rating Summary for Sub Elements"
Wks.Cells(intRow, 2).select
Call SetFont(True, False, 14)
intRow = intRow + 2
intNextSet = intRow
Wks.Cells(intRow, 2) = "Amenity Element Category"
Wks.Cells(intRow, 3) = "Amenity Element Sub-Category"
Wks.Cells(intRow, 4) = "Rating 1"
Wks.Cells(intRow, 5) = "Rating 2"
Wks.Cells(intRow, 6) = "Rating 3"
Wks.Cells(intRow, 7) = "Rating 4"
Wks.Cells(intRow, 8) = "Rating 5"
Wks.Cells(intRow, 9) = "Rating 6"
Wks.Range(Cells(intRow, 2), Cells(intRow, 9)).select
Call SetFont(True, False, 9)
Call SetSurroundHeavy
Set Db = CurrentDb()
Set rst = Db.OpenRecordset("qryAmenitySubElementRatingSummar y",
dbOpenSnapshot)
intRow = intRow + 1
With rst
.MoveFirst
Do While Not .EOF
Wks.Cells(intRow, 2) = !AmenityElementCategory
Wks.Cells(intRow, 3) = !AmenityElementCategorySub
Wks.Cells(intRow, 4) = !Rating1
Wks.Cells(intRow, 5) = !Rating2
Wks.Cells(intRow, 6) = !Rating3
Wks.Cells(intRow, 7) = !Rating4
Wks.Cells(intRow, 8) = !Rating5
Wks.Cells(intRow, 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 SetSurroundHeavy
Wks.Range(Cells(intNextSet, 3), Cells(intRow, 3)).select
Call SetFont(True, False, 9)
Call SetSurroundHeavy
Wks.Range(Cells(intNextSet, 2), Cells(intRow, 9)).select
Call SetSurroundHeavy
Wks.Cells(1, 1).select
End Function