Sub TransferData(QueryName As String, FileName As String, FilePath As String, Password As String)
'This subroutine was created to query information out to Excel File using the Excel.Object to do some formating and to add an Excel File Password on to the file.
Dim XL As Object
Dim WB As Object, WS As Object, Excel As Object
Dim FullPath As String
Dim Test As String
On Error Resume Next
'Check to See if selected path is C:\
If Mid(FilePath, 3) = "\" Then
FullPath = FilePath & FileName
Else
FullPath = FilePath & "\" & FileName
End If
'Delete any old files with same name and output to file
Kill FullPath
'DoCmd.OutputTo acOutputQuery, QueryName, acFormatXLS, FullPath
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, QueryName, FullPath
'Build reference to Excel Objects and open sheet
Set XL = CreateObject("Excel.Application")
XL.Workbooks.Open (FullPath)
XL.Visible = False
'Format Spreadsheet and Close Excel
XL.DisplayAlerts = False
Set WB = XL.Workbooks(FileName)
Set WS = WB.Worksheets(XL.ActiveSheet.Name)
'MsgBox XL.ActiveSheet.Name
WB.Activate
WS.Cells.Select
WS.Cells.EntireColumn.AutoFit
'''''''''''''''''''''''''''''''''''''''''
With WS.Range("A1", WS.Range("A1").End(xlToRight))
.Font.Bold = True
.Interior.ColorIndex = 15
End With
'Auto size the cell sizes
WS.Cells.Select
XL.Selection.ColumnWidth = 30
WS.Cells.EntireColumn.AutoFit
WS.Cells.EntireRow.AutoFit
'Add new Borders
WS.Range("A1", WS.Cells.SpecialCells(xlCellTypeLastCell)).Borders.LineStyle = xlContinuous
'Change Center Header to Sheet Title Name and format it.
'Add Footer Information
'Setup Title Rows
With WS.PageSetup
.CenterHeader = "&""Tahoma,Bold""&14 " & Mid(FileType, 2, 100)
.LeftFooter = "&8&F"
.RightFooter = "&8&P of &N Produced on: " & Date
.PrintTitleRows = "$1:$1"
.CenterHorizontally = True
.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
WS.PageSetup.Orientation = xlLandscape
WS.PageSetup.PaperSize = xlPaperLegal
End With
WB.SaveAs FullPath, , Password
WB.Close
XL.Quit
End Sub]