Results 1 to 3 of 3

Thread: Excel SaveAs from Access Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    1

    Question 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

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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:
    1. Private Sub TestCSV()
    2.  
    3. Call OutputCSV(Application.ActiveSheet, "C:\Test.csv")
    4.  
    5. End Sub
    6.  
    7. Private Sub OutputCSV(oSheet As Excel.Worksheet, sFilename As String)
    8.  
    9.     Dim iFile As Integer
    10.     Dim sLine As String
    11.     Dim lCol As Long
    12.     Dim lRow As Long
    13.    
    14.     iFile = FreeFile
    15.    
    16.     Open sFilename For Output As #iFile
    17.    
    18.     For lRow = oSheet.UsedRange.Row To oSheet.UsedRange.Rows.Count
    19.         For lCol = oSheet.UsedRange.Column To oSheet.UsedRange.Columns.Count
    20.             sLine = sLine & oSheet.Cells(lRow, lCol) & ","
    21.         Next lCol
    22.         Print #iFile, sLine
    23.         sLine = vbNullString
    24.     Next lRow
    25.    
    26.     Close #iFile
    27.  
    28. End Sub

  3. #3
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Excel SaveAs from Access Error

    Quote 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
  •  



Click Here to Expand Forum to Full Width