K
Ken Nguyen
Ok, after a few weeks of trying to figure this out i've finally decided to
ask the pros.
I created a tracking database that allowed for exporting to excel (this is
done and works fine). I also was able to format that same exported excel
sheet that was created and it opens up after it has been formatted (this
works perfectly). The problem I am having is after I export the first excel
file any other exports after doesn't open up. It gets created and semi
formatted only. The first export is formatted in it's entirety to my
specifications, but all others get formatted (not in it's entirety or opens
up). What could be the problem?
Here are parts of my codes (You may use this code for your application as
long as you credit me):
Function: command button to export
'determines the path of where the database is
length = Len(CurrentProject.path)
For X = 1 To length
'finds \ and goes up one level
If Mid(CurrentProject.path, X, 1) = "\" Then
myValue = X
End If
Next X
'stores path to mypath
myPath = Left(CurrentProject.path, myValue)
'request user for a filename
myValue = InputBox("Please give your spreadsheet a filename (Make it
detailed): " & Chr(13) & "For example: 8-12-2007 Report of AB602", "Saving
Spreadsheet File")
'exports to excel using transferspreadsheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"Qry_SpreadsheetONEc", myValue, True
'title of spreadsheet for header
Forms!frmMenu!txtXLStitle = "Sort by Analyst for " & cmbOptions
'Calls function to format the excel workbook
Call ModifyExportedExcelFileFormats(myValue)
Public Sub ModifyExportedExcelFileFormats(sFile As String)
On Error GoTo Err_ModifyExportedExcelFileFormats
'Declare variables
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
'excel application stuff
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(sFile)
For Each xlSheet In xlBook.Worksheets
'Sets the header
rStart = "A1"
rEnd = "Y1"
'Sets the data
cStart = "A2"
cEnd = "Y" & txtNumRecords + 1 'added the plus 1 to include the header
'Sets for repeat columns
rcStart = rStart
rcEnd = "B" & txtNumRecords + 1 'added the plus 1 to include the
header
'freeze panes
xlSheet.Activate
xlSheet.Range("C2", "C2").Select
xlApp.ActiveWindow.FreezePanes = True
'set column widths
xlSheet.Range("A1").ColumnWidth = 9.57
xlSheet.Range("B1").ColumnWidth = 5.5
xlSheet.Range("C1").ColumnWidth = 12.43
xlSheet.Range("D1").ColumnWidth = 9.71
xlSheet.Range("E1").ColumnWidth = 14.57
xlSheet.Range("F1").ColumnWidth = 20
xlSheet.Range("G1").ColumnWidth = 13.5
xlSheet.Range("H1").ColumnWidth = 12.57
xlSheet.Range("I1").ColumnWidth = 16
xlSheet.Range("J1").ColumnWidth = 60
xlSheet.Range("K1").ColumnWidth = 60
xlSheet.Range("L1").ColumnWidth = 10
xlSheet.Range("M1").ColumnWidth = 13
xlSheet.Range("N1").ColumnWidth = 10
xlSheet.Range("O1").ColumnWidth = 10
xlSheet.Range("P1").ColumnWidth = 10
xlSheet.Range("Q1").ColumnWidth = 10
xlSheet.Range("R1").ColumnWidth = 10
xlSheet.Range("S1").ColumnWidth = 10
xlSheet.Range("T1").ColumnWidth = 15
xlSheet.Range("U1").ColumnWidth = 12
xlSheet.Range("V1").ColumnWidth = 15
xlSheet.Range("W1").ColumnWidth = 12
xlSheet.Range("X1").ColumnWidth = 6
xlSheet.Range("Y1").ColumnWidth = 60
'format header row
xlSheet.Range(rStart, rEnd).Font.Bold = True
xlSheet.Range(rStart, rEnd).Interior.ColorIndex = 15
xlSheet.Range(rStart, rEnd).WrapText = True
xlSheet.Range(rStart, rEnd).HorizontalAlignment = xlCenter
'format repeated columns
xlSheet.Range(rcStart, rcEnd).Font.Bold = True
'header row borders
xlSheet.Range(rStart, rEnd).Borders(xlLeft).Weight = xlThin
xlSheet.Range(rStart, rEnd).Borders(xlRight).Weight = xlThin
xlSheet.Range(rStart, rEnd).Borders(xlTop).Weight = xlThin
xlSheet.Range(rStart, rEnd).Borders(xlBottom).Weight = xlThin
'set header row height
xlSheet.Range(rStart).RowHeight = 45
'make columns wrap
xlSheet.Range(rStart, rEnd).WrapText = True
xlSheet.Range(cStart, cEnd).WrapText = True
'Autoformat the cells
xlSheet.Range(rStart, cEnd).Select
Selection.AutoFormat Format:=xlRangeAutoFormatList1, Number:=False,
Font _
:=False, Alignment:=False, Border:=False, Pattern:=True,
Width:=False
'Creates borders around each cell
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.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'Deselects all records and selects the first cell
xlSheet.Range(rStart).Select
'**********************************************
'** SETS UP THE PRINT SETUP
'**********************************************
'Sets page to landscape
xlSheet.PageSetup.Orientation = xlLandscape
'Repeat title rows
xlSheet.PageSetup.PrintTitleRows = "$1:$1"
'Repeat title columns
xlSheet.PageSetup.PrintTitleColumns = "$A:$B"
'Sets the Headers
xlSheet.PageSetup.LeftHeader = ""
xlSheet.PageSetup.CenterHeader = "IAS Tracking System - Redline
Tracking Log" & Chr(10) & "Fiscal Year " & cmbFiscalYear & Chr(10) &
Forms!frmMenu!txtXLStitle
xlSheet.PageSetup.RightHeader = ""
xlSheet.PageSetup.LeftFooter = "California Department of Education"
xlSheet.PageSetup.CenterFooter = "Page &P of &N"
xlSheet.PageSetup.RightFooter = "&D - &T"
'Sets Margins
xlSheet.PageSetup.LeftMargin = 1
xlSheet.PageSetup.RightMargin = 1
xlSheet.PageSetup.TopMargin = 35
xlSheet.PageSetup.BottomMargin = 15
xlSheet.PageSetup.HeaderMargin = 10
xlSheet.PageSetup.FooterMargin = 5
'Sets Print Headings of the Columns i.e. A, B, C
xlSheet.PageSetup.PrintHeadings = False
'Sets Print Gridlines
xlSheet.PageSetup.PrintGridlines = True
xlSheet.PageSetup.PrintComments = xlPrintNoComments
'Printing of the records
xlSheet.PageSetup.CenterHorizontally = False
xlSheet.PageSetup.CenterVertically = False
'Sets page orientation
xlSheet.PageSetup.Orientation = xlLandscape
'Sets the paper size
xlSheet.PageSetup.PaperSize = xlPaperLetter
'Sets page number
xlSheet.PageSetup.FirstPageNumber = xlAutomatic
'Sets the order of the printing
xlSheet.PageSetup.Order = xlOverThenDown
'Sets to black and white printing
xlSheet.PageSetup.BlackAndWhite = False
xlSheet.PageSetup.Zoom = False
'Sets the page to print 1 page by x
xlSheet.PageSetup.FitToPagesWide = 2
xlSheet.PageSetup.FitToPagesTall = 9999
'Prints the errors or not
xlSheet.PageSetup.PrintErrors = xlPrintErrorsDisplayed
Next
'save file
xlBook.Save
'done
xlApp.Quit
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
Exit_ModifyExportedExcelFileFormats:
Call OpenExcelFile(sFile)
Exit Sub
Err_ModifyExportedExcelFileFormats:
vStatusBar = SysCmd(acSysCmdClearStatus)
'MsgBox Err.Number & " - " & Err.Description
Resume Exit_ModifyExportedExcelFileFormats
End Sub
Sub OpenExcelFile(strPathToFile As String)
Dim xlApp As Object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
Set xlApp = GetObject(strPathToFile)
xlApp.Application.Visible = True
xlApp.Parent.Windows(1).Visible = True
End Sub
ask the pros.
I created a tracking database that allowed for exporting to excel (this is
done and works fine). I also was able to format that same exported excel
sheet that was created and it opens up after it has been formatted (this
works perfectly). The problem I am having is after I export the first excel
file any other exports after doesn't open up. It gets created and semi
formatted only. The first export is formatted in it's entirety to my
specifications, but all others get formatted (not in it's entirety or opens
up). What could be the problem?
Here are parts of my codes (You may use this code for your application as
long as you credit me):
Function: command button to export
'determines the path of where the database is
length = Len(CurrentProject.path)
For X = 1 To length
'finds \ and goes up one level
If Mid(CurrentProject.path, X, 1) = "\" Then
myValue = X
End If
Next X
'stores path to mypath
myPath = Left(CurrentProject.path, myValue)
'request user for a filename
myValue = InputBox("Please give your spreadsheet a filename (Make it
detailed): " & Chr(13) & "For example: 8-12-2007 Report of AB602", "Saving
Spreadsheet File")
'exports to excel using transferspreadsheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"Qry_SpreadsheetONEc", myValue, True
'title of spreadsheet for header
Forms!frmMenu!txtXLStitle = "Sort by Analyst for " & cmbOptions
'Calls function to format the excel workbook
Call ModifyExportedExcelFileFormats(myValue)
Public Sub ModifyExportedExcelFileFormats(sFile As String)
On Error GoTo Err_ModifyExportedExcelFileFormats
'Declare variables
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
'excel application stuff
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(sFile)
For Each xlSheet In xlBook.Worksheets
'Sets the header
rStart = "A1"
rEnd = "Y1"
'Sets the data
cStart = "A2"
cEnd = "Y" & txtNumRecords + 1 'added the plus 1 to include the header
'Sets for repeat columns
rcStart = rStart
rcEnd = "B" & txtNumRecords + 1 'added the plus 1 to include the
header
'freeze panes
xlSheet.Activate
xlSheet.Range("C2", "C2").Select
xlApp.ActiveWindow.FreezePanes = True
'set column widths
xlSheet.Range("A1").ColumnWidth = 9.57
xlSheet.Range("B1").ColumnWidth = 5.5
xlSheet.Range("C1").ColumnWidth = 12.43
xlSheet.Range("D1").ColumnWidth = 9.71
xlSheet.Range("E1").ColumnWidth = 14.57
xlSheet.Range("F1").ColumnWidth = 20
xlSheet.Range("G1").ColumnWidth = 13.5
xlSheet.Range("H1").ColumnWidth = 12.57
xlSheet.Range("I1").ColumnWidth = 16
xlSheet.Range("J1").ColumnWidth = 60
xlSheet.Range("K1").ColumnWidth = 60
xlSheet.Range("L1").ColumnWidth = 10
xlSheet.Range("M1").ColumnWidth = 13
xlSheet.Range("N1").ColumnWidth = 10
xlSheet.Range("O1").ColumnWidth = 10
xlSheet.Range("P1").ColumnWidth = 10
xlSheet.Range("Q1").ColumnWidth = 10
xlSheet.Range("R1").ColumnWidth = 10
xlSheet.Range("S1").ColumnWidth = 10
xlSheet.Range("T1").ColumnWidth = 15
xlSheet.Range("U1").ColumnWidth = 12
xlSheet.Range("V1").ColumnWidth = 15
xlSheet.Range("W1").ColumnWidth = 12
xlSheet.Range("X1").ColumnWidth = 6
xlSheet.Range("Y1").ColumnWidth = 60
'format header row
xlSheet.Range(rStart, rEnd).Font.Bold = True
xlSheet.Range(rStart, rEnd).Interior.ColorIndex = 15
xlSheet.Range(rStart, rEnd).WrapText = True
xlSheet.Range(rStart, rEnd).HorizontalAlignment = xlCenter
'format repeated columns
xlSheet.Range(rcStart, rcEnd).Font.Bold = True
'header row borders
xlSheet.Range(rStart, rEnd).Borders(xlLeft).Weight = xlThin
xlSheet.Range(rStart, rEnd).Borders(xlRight).Weight = xlThin
xlSheet.Range(rStart, rEnd).Borders(xlTop).Weight = xlThin
xlSheet.Range(rStart, rEnd).Borders(xlBottom).Weight = xlThin
'set header row height
xlSheet.Range(rStart).RowHeight = 45
'make columns wrap
xlSheet.Range(rStart, rEnd).WrapText = True
xlSheet.Range(cStart, cEnd).WrapText = True
'Autoformat the cells
xlSheet.Range(rStart, cEnd).Select
Selection.AutoFormat Format:=xlRangeAutoFormatList1, Number:=False,
Font _
:=False, Alignment:=False, Border:=False, Pattern:=True,
Width:=False
'Creates borders around each cell
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.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'Deselects all records and selects the first cell
xlSheet.Range(rStart).Select
'**********************************************
'** SETS UP THE PRINT SETUP
'**********************************************
'Sets page to landscape
xlSheet.PageSetup.Orientation = xlLandscape
'Repeat title rows
xlSheet.PageSetup.PrintTitleRows = "$1:$1"
'Repeat title columns
xlSheet.PageSetup.PrintTitleColumns = "$A:$B"
'Sets the Headers
xlSheet.PageSetup.LeftHeader = ""
xlSheet.PageSetup.CenterHeader = "IAS Tracking System - Redline
Tracking Log" & Chr(10) & "Fiscal Year " & cmbFiscalYear & Chr(10) &
Forms!frmMenu!txtXLStitle
xlSheet.PageSetup.RightHeader = ""
xlSheet.PageSetup.LeftFooter = "California Department of Education"
xlSheet.PageSetup.CenterFooter = "Page &P of &N"
xlSheet.PageSetup.RightFooter = "&D - &T"
'Sets Margins
xlSheet.PageSetup.LeftMargin = 1
xlSheet.PageSetup.RightMargin = 1
xlSheet.PageSetup.TopMargin = 35
xlSheet.PageSetup.BottomMargin = 15
xlSheet.PageSetup.HeaderMargin = 10
xlSheet.PageSetup.FooterMargin = 5
'Sets Print Headings of the Columns i.e. A, B, C
xlSheet.PageSetup.PrintHeadings = False
'Sets Print Gridlines
xlSheet.PageSetup.PrintGridlines = True
xlSheet.PageSetup.PrintComments = xlPrintNoComments
'Printing of the records
xlSheet.PageSetup.CenterHorizontally = False
xlSheet.PageSetup.CenterVertically = False
'Sets page orientation
xlSheet.PageSetup.Orientation = xlLandscape
'Sets the paper size
xlSheet.PageSetup.PaperSize = xlPaperLetter
'Sets page number
xlSheet.PageSetup.FirstPageNumber = xlAutomatic
'Sets the order of the printing
xlSheet.PageSetup.Order = xlOverThenDown
'Sets to black and white printing
xlSheet.PageSetup.BlackAndWhite = False
xlSheet.PageSetup.Zoom = False
'Sets the page to print 1 page by x
xlSheet.PageSetup.FitToPagesWide = 2
xlSheet.PageSetup.FitToPagesTall = 9999
'Prints the errors or not
xlSheet.PageSetup.PrintErrors = xlPrintErrorsDisplayed
Next
'save file
xlBook.Save
'done
xlApp.Quit
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
Exit_ModifyExportedExcelFileFormats:
Call OpenExcelFile(sFile)
Exit Sub
Err_ModifyExportedExcelFileFormats:
vStatusBar = SysCmd(acSysCmdClearStatus)
'MsgBox Err.Number & " - " & Err.Description
Resume Exit_ModifyExportedExcelFileFormats
End Sub
Sub OpenExcelFile(strPathToFile As String)
Dim xlApp As Object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
Set xlApp = GetObject(strPathToFile)
xlApp.Application.Visible = True
xlApp.Parent.Windows(1).Visible = True
End Sub