J
John
I have read through several of the topics on this subject and still don't
seem to have it correct. Will someone give this a quick read and see if you
can spot my problem? Sometimes this runs ok and other times I get odd
errors...
===========
'Run the selected query out to excel...
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, RunQry, fn, True
'Format the spreadsheet
Dim xlApp As Object
Dim xlWkb As Object
Dim xlSht As Object
Dim FlNm As String 'Just the name of the file.
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWkb = xlApp.Workbooks.Open(fn) ' "fn" is the name
with path.
Set xlSht = xlApp.ActiveWorkbook.Worksheets(1)
FlNm = xlApp.ActiveWorkbook.name
'Turn off alerts, macros, screen updating
xlApp.DisplayAlerts = False
xlApp.Interactive = False
xlApp.ScreenUpdating = False
'Make sure the sheet is active
xlSht.Activate
'Format the worksheet
xlSht.Range("A1").Select
xlSht.Range(Selection,
Selection.End(xlToRight)).Select
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
Selection.Font.ColorIndex = 1
Selection.Font.Bold = True
With Selection.Font
.name = "Arial"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.UnderLine = xlUnderlineStyleNone
.ColorIndex = 1
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
xlSht.Cells.Select
xlSht.Cells.EntireColumn.AutoFit
xlSht.Range("A2").Select
ActiveWindow.FreezePanes = True
xlSht.Range("A1").Select
xlSht.Range(Selection,
ActiveCell.SpecialCells(xlLastCell)).Select
Selection.AutoFilter
xlSht.Range("A2").Select
'Rename the sheet to the file name.
Sheets(1).name = Left(Trim(FlNm), Len(Trim(FlNm)) - 4)
'Save the workbook, clean-up, and exit
xlApp.ActiveWorkbook.Save
xlApp.DisplayAlerts = True
xlApp.Interactive = True
xlApp.ScreenUpdating = True
Set xlSht = Nothing
xlWkb.Close
Set xlWkb = Nothing
xlApp.Quit
Set xlApp = Nothing
==========
I was also having trouble getting the excel application to close all the
way. You can see it if you look in the task manager.
seem to have it correct. Will someone give this a quick read and see if you
can spot my problem? Sometimes this runs ok and other times I get odd
errors...
===========
'Run the selected query out to excel...
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, RunQry, fn, True
'Format the spreadsheet
Dim xlApp As Object
Dim xlWkb As Object
Dim xlSht As Object
Dim FlNm As String 'Just the name of the file.
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWkb = xlApp.Workbooks.Open(fn) ' "fn" is the name
with path.
Set xlSht = xlApp.ActiveWorkbook.Worksheets(1)
FlNm = xlApp.ActiveWorkbook.name
'Turn off alerts, macros, screen updating
xlApp.DisplayAlerts = False
xlApp.Interactive = False
xlApp.ScreenUpdating = False
'Make sure the sheet is active
xlSht.Activate
'Format the worksheet
xlSht.Range("A1").Select
xlSht.Range(Selection,
Selection.End(xlToRight)).Select
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
Selection.Font.ColorIndex = 1
Selection.Font.Bold = True
With Selection.Font
.name = "Arial"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.UnderLine = xlUnderlineStyleNone
.ColorIndex = 1
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
xlSht.Cells.Select
xlSht.Cells.EntireColumn.AutoFit
xlSht.Range("A2").Select
ActiveWindow.FreezePanes = True
xlSht.Range("A1").Select
xlSht.Range(Selection,
ActiveCell.SpecialCells(xlLastCell)).Select
Selection.AutoFilter
xlSht.Range("A2").Select
'Rename the sheet to the file name.
Sheets(1).name = Left(Trim(FlNm), Len(Trim(FlNm)) - 4)
'Save the workbook, clean-up, and exit
xlApp.ActiveWorkbook.Save
xlApp.DisplayAlerts = True
xlApp.Interactive = True
xlApp.ScreenUpdating = True
Set xlSht = Nothing
xlWkb.Close
Set xlWkb = Nothing
xlApp.Quit
Set xlApp = Nothing
==========
I was also having trouble getting the excel application to close all the
way. You can see it if you look in the task manager.