All,
I have a Public Function in Access that will read from my tables and create a formatted Excel Spreadsheet. It works most of the time, other times I get errors at various spots Object variable or With block variable not set, Error 91, Application-defined or Object-defined error, Error 1004. I think it could be the way I am call Excel, but I am not sure this is my first attempt at doing this. I have do a lot of VBA programing in Access and have not done much in Excel in a long time.
If I change xlApp.Visible = True to False, I get Object variable or With block variable not set, Error 91 99% of the time.
Here is my code. I put a note in on the two lines I get the errors on sometimes. I can run this code a few times with out an issue and then I run it again without changing anything and I get on of the errors.
I am up for suggestions.
TIA,
Rodger
I have a Public Function in Access that will read from my tables and create a formatted Excel Spreadsheet. It works most of the time, other times I get errors at various spots Object variable or With block variable not set, Error 91, Application-defined or Object-defined error, Error 1004. I think it could be the way I am call Excel, but I am not sure this is my first attempt at doing this. I have do a lot of VBA programing in Access and have not done much in Excel in a long time.
If I change xlApp.Visible = True to False, I get Object variable or With block variable not set, Error 91 99% of the time.
Here is my code. I put a note in on the two lines I get the errors on sometimes. I can run this code a few times with out an issue and then I run it again without changing anything and I get on of the errors.
I am up for suggestions.
TIA,
Rodger
Code:
Option Compare Database
Public Function CreateExcel(Optional myEmpoyeeNumber As Integer)
Dim xlApp As Object
Dim myMessage, myStyle, myTitle, myResult
Dim mySQL, myDB, myRS1, myRS2
Dim myColorIndex As Integer
Dim myColor, x, y, z
Dim myValue1, myValue2, myStatus1, myStatus2
Dim myProjectManager, myLeadAnalyst, myTSG, myEmpSQL
'On Error GoTo Err_CreateExcel
'Create Excel Workbook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
xlApp.Workbooks.Add
Set myDB = CurrentDb()
mySQL = "SELECT * FROM dbo_CATEGORIES"
Set myRS2 = myDB.OpenRecordset(mySQL, dbOpenDynaset, dbSeeChanges)
x = 1
y = 7
z = xlApp.Worksheets(1).UsedRange.Rows.Count
'Rename the sheet
If Nz(myEmpoyeeNumber, "0") = "0" Then
xlApp.Worksheets(1).Name = "All Projects"
Else
xlApp.Worksheets(1).Name = myEmpoyeeNumber
End If
Do Until myRS2.EOF
myColorIndex = myRS2!CAT_COLOR_INDEX
xlApp.Worksheets(1).Cells(x, y).Select
With Selection
.Interior.ColorIndex = myColorIndex [COLOR=red]'<< Runtime 91 sometimes[/color]
.Interior.Pattern = xlSolid
End With
xlApp.Worksheets(1).Cells(x, y + 1).Value = myRS2.CAT_DESCRIPTION
x = x + 1
myRS2.MoveNext
Loop
'Create the headers
xlApp.Worksheets(1).Cells(1, 2).Value = "INFORMATION SYSTEMS PROJECTS"
xlApp.Worksheets(1).Cells(1, 3).Value = Date
xlApp.Worksheets(1).Cells(6, 2).Value = "Project"
xlApp.Worksheets(1).Cells(6, 3).Value = "Customer"
xlApp.Worksheets(1).Cells(6, 4).Value = "Original Hours Estimated"
xlApp.Worksheets(1).Cells(6, 5).Value = "Actual Hours Expended"
xlApp.Worksheets(1).Cells(6, 6).Value = "Estimated Hours Remaining"
xlApp.Worksheets(1).Cells(6, 7).Value = "Original Target Completion"
xlApp.Worksheets(1).Cells(6, 8).Value = "New Target Completion"
xlApp.Worksheets(1).Cells(6, 9).Value = "Resources"
xlApp.Worksheets(1).Cells(6, 10).Value = "Comments"
xlApp.Worksheets(1).Cells(7, 3).Select
ActiveWindow.FreezePanes = True
Rows("6:6").RowHeight = 33.75
Columns("A:A").ColumnWidth = 3
Columns("B:B").ColumnWidth = 49.43
Columns("C:C").ColumnWidth = 27.29
Columns("D:D").ColumnWidth = 10
Columns("E:E").ColumnWidth = 11.14
Columns("F:F").ColumnWidth = 12.57
Columns("G:G").ColumnWidth = 11.86
Columns("H:H").ColumnWidth = 14.29
Columns("I:I").ColumnWidth = 32
Columns("J:J").ColumnWidth = 12
xlApp.Worksheets(1).Range(Cells(6, 2), Cells(6, 10)).Select [COLOR=red]'<< Runtime 1004 sometimes [/color]
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
If IsNull(myEmpoyeeNumber) Or myEmpoyeeNumber = 0 Then
myEmpSQL = ""
Else
myEmpSQL = "AND ((PM.EMP_NUMBER)=" & myEmpoyeeNumber & ") OR (((LAN.EMP_NUMBER)=" & myEmpoyeeNumber & ")) OR (((TSG.EMP_NUMBER)=" & myEmpoyeeNumber & ")) OR (((EmployeeSingleLineNumber([P].[PRO_ID])) In ('" & myEmpoyeeNumber & "'))) "
End If
mySQL = "SELECT S.STA_ID, S.STA_DESCRIPTION, P.PRO_LINE_NUMBER, P.PRO_NAME, C.CAT_DESCRIPTION, C.CAT_COLOR_INDEX, C.CAT_COLOR, PP.PAP_ID, PP.PAP_DESCRIPTION, PM.NAME_LF AS PROJECT_MANAGER, LAN.NAME_LF AS LEAD_ANALYST, TSG.NAME_LF AS TSG, PHO.PROJECT_HOURS_ORIGINAL, PHC.PROJECT_HOURS_CURRENT, PSDO.PROJECT_START_DATE_ORIGINAL, PSDC.PROJECT_START_DATE_CURRENT, P.PRO_ORIGINAL_DATE, CustomerSingleLine(P.PRO_ID) AS CUSTOMER, EmployeeSingleLine(P.PRO_ID) AS RESOURCES, PH.HOURS, PH.HOURS_EXPENDED, PH.HORS_LEFT, PM.EMP_NUMBER AS PROJECT_MANAGER_NUMBER, LAN.EMP_NUMBER AS LEAD_ANALYST_NUMBER, TSG.EMP_NUMBER AS TSG_NUMBER, EmployeeSingleLineNumber([P].[PRO_ID]) AS RESOURCES_NUMBER " & _
"FROM ((((((((((dbo_PROJECTS AS P LEFT JOIN qry_Project_Hours_Current AS PHC ON P.PRO_ID = PHC.PRH_PRO_ID) LEFT JOIN qry_Project_Hours_Original AS PHO ON P.PRO_ID = PHO.PRH_PRO_ID) LEFT JOIN qry_Project_Start_Date_Original AS PSDO ON P.PRO_ID = PSDO.PSD_PRO_ID) LEFT JOIN qry_Project_Start_Date_Current AS PSDC ON P.PRO_ID = PSDC.PSD_PRO_ID) LEFT JOIN qry_Employee_Names AS PM ON P.PRO_PM_ID = PM.EMP_NUMBER) LEFT JOIN qry_Employee_Names AS LAN ON P.PRO_LAN_ID = LAN.EMP_NUMBER) LEFT JOIN qry_Employee_Names AS TSG ON P.PRO_TSG_ID = TSG.EMP_NUMBER) LEFT JOIN dbo_PARENT_PROJECTS AS PP ON P.PRO_PAP_ID = PP.PAP_ID) LEFT JOIN dbo_STATUS AS S ON P.PRO_STA_ID = S.STA_ID) LEFT JOIN dbo_CATEGORIES AS C ON P.PRO_CAT_ID = C.CAT_ID) LEFT JOIN qry_ProjectHours_Step3 AS PH ON P.PRO_ID = PH.PRO_ID " & _
"WHERE ((P.PRO_LINE_NUMBER)<>'1000') " & myEmpSQL & _
"ORDER BY S.STA_ID, P.PRO_LINE_NUMBER, P.PRO_NAME;"
Set myRS1 = myDB.OpenRecordset(mySQL, dbOpenDynaset, dbSeeChanges)
x = 7
y = 1
myStatus1 = myRS1!STA_DESCRIPTION
Do Until myRS1.EOF
If myStatus1 <> myStatus2 Then
xlApp.Worksheets(1).Cells(x, y + 1).Value = myStatus1
xlApp.Worksheets(1).Cells(x, y + 1).Select
With Selection
.Font.Bold = True
.HorizontalAlignment = xlCenter
End With
x = x + 1
Else
'Does the project have a status if yes put the color index in the first cell for the row.
If IsNull(myRS1!CAT_COLOR_INDEX) Then
Else
myColorIndex = myRS1!CAT_COLOR_INDEX
'xlApp.Worksheets(1).Range(Cells(x, y), Cells(x, y)).Select
xlApp.Worksheets(1).Cells(x, y).Select
With Selection
.Interior.ColorIndex = myColorIndex
.Interior.Pattern = xlSolid
End With
End If
'Start listing out the project.
xlApp.Worksheets(1).Cells(x, y + 1).Value = myRS1!PRO_LINE_NUMBER & " - " & myRS1!PRO_NAME
xlApp.Worksheets(1).Cells(x, y + 2).Value = myRS1!CUSTOMER
xlApp.Worksheets(1).Cells(x, y + 3).Value = myRS1!HOURS
xlApp.Worksheets(1).Cells(x, y + 4).Value = myRS1!HOURS_EXPENDED
xlApp.Worksheets(1).Cells(x, y + 5).Value = myRS1!HORS_LEFT
xlApp.Worksheets(1).Cells(x, y + 6).Value = myRS1!PROJECT_START_DATE_ORIGINAL
xlApp.Worksheets(1).Cells(x, y + 7).Value = myRS1!PROJECT_START_DATE_CURRENT
'Get Project Manager, Lead Analyst, TSG ro add to the resources section
If IsNull(myRS1!PROJECT_MANAGER) Or myRS1!PROJECT_MANAGER = "" Then
myProjectManger = ""
Else
If IsNull(myRS1!RESOURCES) Or myRS1!RESOURCES = "" Then
myProjectManger = myRS1!PROJECT_MANAGER
Else
myProjectManger = myRS1!PROJECT_MANAGER & ", "
End If
End If
If IsNull(myRS1!LEAD_ANALYST) Or myRS1!LEAD_ANALYST = "" Then
myLeadAnalyst = ""
Else
If IsNull(myRS1!RESOURCES) Or myRS1!RESOURCES = "" Then
myLeadAnalyst = myRS1!LEAD_ANALYST
Else
myLeadAnalyst = myRS1!LEAD_ANALYST & ", "
End If
End If
If IsNull(myRS1!TSG) Or myRS1!TSG = "" Then
myTSG = ""
Else
If IsNull(myRS1!RESOURCES) Or myRS1!RESOURCES = "" Then
myTSG = myRS1!TSG
Else
myTSG = myRS1!TSG & ", "
End If
End If
xlApp.Worksheets(1).Cells(x, y + 8).Value = myProjectManager & myLeadAnalyst & myTSG & myRS1!RESOURCES
'xlApp.Worksheets(1).Cells(x, y + 1).Select
End If
x = x + 1
y = 1
myStatus1 = myRS1!STA_DESCRIPTION
myRS1.MoveNext
If myRS1.EOF Then
Else
myStatus2 = myRS1!STA_DESCRIPTION
End If
'MsgBox ("myStatus1 = " & myStatus1 & vbCrLf & "myStatus2 = " & myStatus2)
Loop
'Right align hours
xlApp.Worksheets(1).Range(Cells(8, 4), Cells(x, 6)).Select
With Selection
.HorizontalAlignment = xlRight
End With
'Date format
xlApp.Worksheets(1).Range(Cells(8, 7), Cells(x, 8)).Select
Selection.NumberFormat = "m/d/yyyy;@"
'Wrap text for Resources
xlApp.Worksheets(1).Range(Cells(8, 9), Cells(x, 9)).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
z = xlApp.Worksheets(1).UsedRange.Rows.Count
xlApp.Worksheets(1).Cells(7, 3).Select
MsgBox "Export Completed. Number of rows " & z
xlApp.Visible = True
Set xlApp = Nothing
Exit_CreateExcel:
Exit Function
Err_CreateExcel:
Beep
MsgBox Err.Number & " - " & Err.Description
'Resume Exit_CreateExcel
Exit Function
End Function