|
-
Feb 2nd, 2005, 11:07 AM
#1
Thread Starter
New Member
Excel SaveAs from Access Error
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
-
Feb 2nd, 2005, 03:20 PM
#2
Re: Excel SaveAs from Access Error
You can always bypass Excel's SaveAs method entirely and write a .csv youself something like this:
VB Code:
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
-
Feb 3rd, 2005, 10:03 AM
#3
Re: Excel SaveAs from Access Error
 Originally Posted by JeepNC
Any ideas as to what the problem is?
BTW, when you save a book to a flat file, it can only have one sheet.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|