' Creates the Excel App and workbook, and adds the sheet
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.workbook
Dim xlWorksheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlWorkbook = xlApp.Workbooks.Add
Set xlWorksheet = xlWorkbook.Sheets.Add
' The row index which each portion of the report begins on
Dim intReportTitleRowIndex As Integer
Dim intReportStartIndex As Integer
Dim intReportHeaderIndex As Integer
intReportTitleRowIndex = 3
intReportHeaderIndex = 5
intReportStartIndex = 7
' Sets the Page Orientation and where the page break for printing will be
xlWorksheet.PageSetup.Orientation = xlLandscape
Dim qryDef As QueryDef
Dim db As Database
Set db = CurrentDb
' Query to pull back the data (temporary - deleted after the records are pulled back)
Dim qryName As String
qryName = "qryPIFLogHighLevelExport"
' Creates the query
db.CreateQueryDef (qryName)
Set qryDef = db.QueryDefs(qryName)
qryDef.SQL = "SELECT [Performance Improvement].[PIF Closure Date], [Performance Improvement].[Submission Date]," & _
"[Performance Improvement].Initiator, [Performance Improvement].[Area Owner]," & _
"[Performance Improvement].[Results Summary], [Performance Improvement].Comment," & _
"[Performance Improvement].Number, [Performance Improvement].[Problem/Suggestion Summary]," & _
"[Performance Improvement].[PIF Type], [Performance Improvement].[PIF Owner]," & _
"[Performance Improvement].[PIF Closure Date], [Performance Improvement].[Web PIF Number]" & _
"From [Performance Improvement] WHERE " & getWhereClause & " ORDER BY [Performance Improvement].[Evaluation Due Date]"
' Pulls back the data using the query
Dim rs As Recordset
Set rs = db.OpenRecordset(qryName)
' Format and Label the Report date range
xlWorksheet.Range(xlWorksheet.Cells(1, 1), xlWorksheet.Cells(1, 2)).merge
xlWorksheet.Range(xlWorksheet.Cells(1, 3), xlWorksheet.Cells(1, 5)).merge
xlWorksheet.Cells(1, 3).horizontalalignment = -4131
xlWorksheet.Cells(1, 1).Value = "Report Period:"
xlWorksheet.Cells(1, 3).Value = " " & formatDateRange
' Format and label the Date of the Report
xlWorksheet.Range(xlWorksheet.Cells(2, 1), xlWorksheet.Cells(2, 2)).merge
xlWorksheet.Range(xlWorksheet.Cells(2, 3), xlWorksheet.Cells(2, 5)).merge
xlWorksheet.Cells(2, 3).horizontalalignment = -4131
xlWorksheet.Cells(2, 1).Value = "Report Date:"
xlWorksheet.Cells(2, 3).Value = CStr(" " & MonthName(Month(CDate(Now()))) & " " & Day(CDate(Now())) & ", " & Year(CDate(Now())))
' Format Title of Report
xlWorksheet.Range(xlWorksheet.Cells(intReportTitleRowIndex, 1), xlWorksheet.Cells(intReportTitleRowIndex, 8)).merge
xlWorksheet.rows(intReportTitleRowIndex).RowHeight = 20
xlWorksheet.Cells(intReportTitleRowIndex, 1).Value = "Performance Improvement Form (PIF) Log: High Level Summary of Submitted PIF's and Results"
xlWorksheet.Cells(intReportTitleRowIndex, 1).Font.bold = True
xlWorksheet.Cells(intReportTitleRowIndex, 1).Font.Size = 14
xlWorksheet.Cells(intReportTitleRowIndex, 1).verticalalignment = -4108
xlWorksheet.Cells(intReportTitleRowIndex, 1).horizontalalignment = -4108
' Column Header font-weight: Bold, size: 12
xlWorksheet.Range(xlWorksheet.Cells(intReportHeaderIndex, 1), xlWorksheet.Cells(intReportHeaderIndex, 8)).Font.bold = True
xlWorksheet.Range(xlWorksheet.Cells(intReportHeaderIndex, 1), xlWorksheet.Cells(intReportHeaderIndex, 8)).Font.Size = 12
' Column Header Vertical-Alignment: center and border style(top and bottom only): double line
xlWorksheet.Range(xlWorksheet.Cells(intReportHeaderIndex, 1), xlWorksheet.Cells(intReportHeaderIndex, 8)).verticalalignment = -4108
xlWorksheet.Range(xlWorksheet.Cells(intReportHeaderIndex, 1), xlWorksheet.Cells(intReportHeaderIndex, 8)).borders(8).linestyle = -4119
xlWorksheet.Range(xlWorksheet.Cells(intReportHeaderIndex, 1), xlWorksheet.Cells(intReportHeaderIndex, 8)).borders(9).linestyle = -4119
' Delete the query as it is unneeded now
db.QueryDefs.Delete (qryName)
' Sets up the column headers (size and labels)
xlWorksheet.Cells(intReportHeaderIndex, 1).Value = "PIF #"
xlWorksheet.Columns("A:A").ColumnWidth = 7
xlWorksheet.Cells(intReportHeaderIndex, 2).Value = "Date Submitted"
xlWorksheet.Columns("B:B").ColumnWidth = 12
xlWorksheet.Cells(intReportHeaderIndex, 3).Value = "Submitter"
xlWorksheet.Columns("C:C").ColumnWidth = 16
xlWorksheet.Cells(intReportHeaderIndex, 4).Value = "Type"
xlWorksheet.Columns("D

").ColumnWidth = 7
xlWorksheet.Cells(intReportHeaderIndex, 5).Value = "Issue Summary"
xlWorksheet.Columns("E:E").ColumnWidth = 35
xlWorksheet.Cells(intReportHeaderIndex, 6).Value = "Owner"
xlWorksheet.Columns("F:F").ColumnWidth = 25
xlWorksheet.Cells(intReportHeaderIndex, 7).Value = "Status/Results,Summary/Comments"
xlWorksheet.Columns("G:G").ColumnWidth = 40
xlWorksheet.Cells(intReportHeaderIndex, 8).Value = "Date Closed"
xlWorksheet.Columns("H:H").ColumnWidth = 12
xlWorksheet.Columns("A:H").wraptext = True
' Loop through the PIF records and add each to the excel sheet
Dim intActiveRow As Integer
intActiveRow = intReportStartIndex
While Not rs.EOF
' Sets solid line bottom border for the current row (divider between PIFs)
xlWorksheet.Range(xlWorksheet.Cells(intReportStartIndex, 1), xlWorksheet.Cells(intActiveRow, 8)).borders(9).weight = 2
' Sets cells to have center vertical alignment and left horizontal alignment
xlWorksheet.Range(xlWorksheet.Cells(intReportStartIndex, 1), xlWorksheet.Cells(intActiveRow, 8)).verticalalignment = -4160
xlWorksheet.Range(xlWorksheet.Cells(intReportStartIndex, 1), xlWorksheet.Cells(intActiveRow, 8)).horizontalalignment = -4131
' Sets font for cells
xlWorksheet.Range(xlWorksheet.Cells(intReportStartIndex, 1), xlWorksheet.Cells(intActiveRow, 8)).Font.Size = 10
xlWorksheet.Range(xlWorksheet.Cells(intReportStartIndex, 1), xlWorksheet.Cells(intActiveRow, 8)).Font.Name = "arial"
xlWorksheet.Cells(intActiveRow, 1).Value = rs.Fields("Number").Value
' Retrieves the date without the time
Dim intSpaceIndex As Integer
Dim strDateString As String
strDateString = CStr(rs.Fields("Submission Date").Value)
intSpaceIndex = InStr(strDateString, " ")
xlWorksheet.Cells(intActiveRow, 2).Value = Left(strDateString, intSpaceIndex)
xlWorksheet.Cells(intActiveRow, 3).Value = rs.Fields("Initiator").Value
xlWorksheet.Cells(intActiveRow, 4).Value = getAbbrevPIFType(rs.Fields("PIF Type").Value)
xlWorksheet.Cells(intActiveRow, 5).Value = rs.Fields("Problem/Suggestion Summary").Value & vbLf
xlWorksheet.Cells(intActiveRow, 6).Value = rs.Fields("PIF Owner").Value
xlWorksheet.Cells(intActiveRow, 7).Value = rs.Fields("Results Summary").Value
xlWorksheet.Cells(intActiveRow, 8).Value = rs.Fields("PIF Closure Date").Value
rs.MoveNext
intActiveRow = intActiveRow + 1
Wend
'ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
'MsgBox xlWorksheet.VPageBreaks.count
xlWorksheet.VPageBreaks(1).Location = xlWorksheet.Columns("i")
xlApp.Visible = True
Set xlWorkbook = Nothing
Set xlWorksheet = Nothing
Set xlApp = Nothing