PDA

Click to See Complete Forum and Search --> : Excel SaveAs from Access Error


JeepNC
Feb 2nd, 2005, 10:07 AM
I have the following code running trying to open an excel sheet, change the formatting on a column and then save the sheet as a text file. When it gets to the SaveAs I get an error message: SaveAs Method of Workbook Class Failed. I have narrowed it down to the FileFormat:xlText as the culprit, if you take out the file format it saves fine, but the file is just garbage. Any ideas as to what the problem is?
Thanks!

Dim xlx As Object, xlw As Object, xls As Object, xlc As Object

Set xlx = CreateObject("excel.application")
xlx.Visible = True
Set xlw = xlx.workbooks.Open("C:\book1.xls")
Set xls = xlw.worksheets("sheet1")
xls.Columns("K:K").NumberFormat = "0"
xlw.SaveAs Filename:="C:\Book1.txt", _
FileFormat:=xlText, CreateBackup:=False
Set xlc = Nothing
Set xls = Nothing
xlw.Close False
Set xlw = Nothing
xlx.Quit
Set xlx = Nothing

Comintern
Feb 2nd, 2005, 02:20 PM
You can always bypass Excel's SaveAs method entirely and write a .csv youself something like this:

Private Sub TestCSV()

Call OutputCSV(Application.ActiveSheet, "C:\Test.csv")

End Sub

Private Sub OutputCSV(oSheet As Excel.Worksheet, sFilename As String)

Dim iFile As Integer
Dim sLine As String
Dim lCol As Long
Dim lRow As Long

iFile = FreeFile

Open sFilename For Output As #iFile

For lRow = oSheet.UsedRange.Row To oSheet.UsedRange.Rows.Count
For lCol = oSheet.UsedRange.Column To oSheet.UsedRange.Columns.Count
sLine = sLine & oSheet.Cells(lRow, lCol) & ","
Next lCol
Print #iFile, sLine
sLine = vbNullString
Next lRow

Close #iFile

End Sub

Comintern
Feb 3rd, 2005, 09:03 AM
Any ideas as to what the problem is?

BTW, when you save a book to a flat file, it can only have one sheet.