Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3
This is the forum to discuss the Wrox book Access 2007 VBA Programmer's Reference by Teresa Hennig, Rob Cooper, Geoffrey L. Griffith, Armen Stein; ISBN: 9780470047033
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 18th, 2009, 03:16 PM
Registered User
 
Join Date: Jan 2008
Location: Limestone, ME, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel Formatting from Access

I am exporting a spreadsheet from Access and then formatting the spreadsheet. If I open the database and export the file it works the first time. If, however, I leave the database open and try to export a second time, it errs out "Runtime Error 1004 - Method "Cells" of Object "_Global" Failed. Here's the code. It errs on the line with the angry face. What am I missing?

[code]
Public Function ContLiab()
Dim GetDlg As FileDialog
Dim XLApp As Excel.Application
Dim MyFile As String
Set MyDb = CurrentDb
'ReportPath = "K:\JAMAA\Contingent Liabilities"
ReportPath = CurrentProject.Path
ReportFile = "\ContingentLiabilities_"
ReportDate = Format(Date, "mm-dd-yy")
varX = 1
ReportFile = ReportPath & ReportFile & ReportDate & ".xls"
MyDb.Execute "DELETE tbl_Contliab.* FROM tbl_Contliab;"

Set GetDlg = Application.FileDialog(msoFileDialogOpen)
GetDlg.Title = "Please select the raw data file to import"
GetDlg.InitialFileName = ReportPath & "\ContLiab.txt"
GetDlg.ButtonName = "Import"
GetDlg.Show
MyFile = GetDlg.SelectedItems.Item(1)

DoCmd.TransferText acImportFixed, "Contliab", "tbl_Contliab", MyFile
FileCopy ReportPath & "\Contingent Liabilities_TPL.xls", ReportFile
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "CL_Comm", ReportFile, True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "CL_NoComm", ReportFile, True

''************ FORMATTING
Set XLApp = New Excel.Application
With XLApp
' 'Open the file that was selected so that we can play with it.
'
' '******************************Format External Data Worksheet ***************************************
' '.Visible = True
.Workbooks.Open ReportFile

.Sheets("Guidelines").Select
.Range("C14:M14").Select
.ActiveCell.FormulaR1C1 = "Contents Data Current As of: " & Format(Now, "dd mmm, yyyy - hh:nn AMPM")
.ActiveCell.Characters(Start:=1, Length:=40).Font.Name = "Arial"
.ActiveCell.Characters(Start:=1, Length:=40).Font.FontStyle = "Bold"
.ActiveCell.Characters(Start:=1, Length:=40).Font.Size = 10
.ActiveCell.Characters(Start:=1, Length:=40).Font.Strikethrough = False
.ActiveCell.Characters(Start:=1, Length:=40).Font.Superscript = False
.ActiveCell.Characters(Start:=1, Length:=40).Font.Subscript = False
.ActiveCell.Characters(Start:=1, Length:=40).Font.OutlineFont = False
.ActiveCell.Characters(Start:=1, Length:=40).Font.Shadow = False
.ActiveCell.Characters(Start:=1, Length:=40).Font.Underline = xlUnderlineStyleNone
.ActiveCell.Characters(Start:=1, Length:=40).Font.ColorIndex = xlAutomatic
.Range("C15").Select

.Sheets("CL_Comm").Select
.Range("A1:Y1").Select
.Selection.Font.Bold = True
.Columns("A:Y").Select
.Columns("A:Y").EntireColumn.AutoFit
.ActiveCell.SpecialCells(xlLastCell).Select
.Range(Selection, Cells(1)).Select
.Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
.Range("A2").Select
.ActiveWindow.FreezePanes = True
.Range("A1:Y1").Select
.Selection.Interior.ColorIndex = 34
.Selection.Interior.Pattern = xlSolid

.ActiveCell.SpecialCells(xlLastCell).Select
.Range(Selection, Cells(1)).Select
.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
.Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Selection.Borders(xlEdgeLeft).Weight = xlThin
.Selection.Borders(xlEdgeLeft).ColorIndex = xlAutomatic
.Selection.Borders(xlEdgeTop).LineStyle = xlContinuous
.Selection.Borders(xlEdgeTop).Weight = xlThin
.Selection.Borders(xlEdgeTop).ColorIndex = xlAutomatic
.Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Selection.Borders(xlEdgeBottom).Weight = xlThin
.Selection.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
.Selection.Borders(xlEdgeRight).LineStyle = xlContinuous
.Selection.Borders(xlEdgeRight).Weight = xlThin
.Selection.Borders(xlEdgeRight).ColorIndex = xlAutomatic
.Selection.Borders(xlInsideVertical).LineStyle = xlContinuous
.Selection.Borders(xlInsideVertical).Weight = xlThin
.Selection.Borders(xlInsideVertical).ColorIndex = xlAutomatic
.Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Selection.Borders(xlInsideHorizontal).Weight = xlThin
.Selection.Borders(xlInsideHorizontal).ColorIndex = xlAutomatic

.Sheets("CL_NoComm").Select
.Range("A1:Y1").Select
.Selection.Font.Bold = True
.Columns("A:Y").Select
.Columns("A:Y").EntireColumn.AutoFit
.ActiveCell.SpecialCells(xlLastCell).Select
.Range(Selection, Cells(1)).Select
.Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
.Range("A2").Select
.ActiveWindow.FreezePanes = True
.Range("A1:Y1").Select
.Selection.Interior.ColorIndex = 34
.Selection.Interior.Pattern = xlSolid

.ActiveCell.SpecialCells(xlLastCell).Select
.Range(Selection, Cells(1)).Select
.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
.Selection.Borders(xlDiagonalUp).LineStyle = xlNone

.Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Selection.Borders(xlEdgeLeft).Weight = xlThin
.Selection.Borders(xlEdgeLeft).ColorIndex = xlAutomatic

.Selection.Borders(xlEdgeTop).LineStyle = xlContinuous
.Selection.Borders(xlEdgeTop).Weight = xlThin
.Selection.Borders(xlEdgeTop).ColorIndex = xlAutomatic

.Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Selection.Borders(xlEdgeBottom).Weight = xlThin
.Selection.Borders(xlEdgeBottom).ColorIndex = xlAutomatic

.Selection.Borders(xlEdgeRight).LineStyle = xlContinuous
.Selection.Borders(xlEdgeRight).Weight = xlThin
.Selection.Borders(xlEdgeRight).ColorIndex = xlAutomatic

.Selection.Borders(xlInsideVertical).LineStyle = xlContinuous
.Selection.Borders(xlInsideVertical).Weight = xlThin
.Selection.Borders(xlInsideVertical).ColorIndex = xlAutomatic

.Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Selection.Borders(xlInsideHorizontal).Weight = xlThin
.Selection.Borders(xlInsideHorizontal).ColorIndex = xlAutomatic

.Range("A2").Select
.Sheets("CL_Comm").Select
.Range("A2").Select

.Sheets("Guidelines").Select
.Range("A2").Select
.ActiveWorkbook.Save
.Quit
End With
Set XLApp = Nothing

MsgBox "Your file has been exported to " & ReportFile, vbOKOnly + vbInformation, "CSR MGT Notices"
End Function
[\code]


Similar Threads
Thread Thread Starter Forum Replies Last Post
EXPORT DATAGRID TO EXCEL FORMATTING PROBLEM akshay144 VS.NET 2002/2003 2 October 31st, 2006 01:10 PM
Help with formatting an excel spreadsheet lushh Access 0 October 1st, 2006 10:48 PM
ASP to Excel - excel output formatting issue mat41 Classic ASP Professional 0 August 13th, 2006 06:41 AM
Export to Excel Formatting/Hyperlink HockeyMom SQL Server DTS 0 June 5th, 2006 04:04 PM
Formatting Excel through VBA John Anthony Access VBA 0 July 31st, 2003 05:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.