Solved

VBA: Saving Excel as Text file

Posted on 2004-08-25
8
2,198 Views
Last Modified: 2012-06-21
I want to save an excel file as a text file in VBA:

xlFile.SaveAs filename:=outputFile, FileFormat:=xlText

The problem is it is enclosing the double quote character in quotes in the saved file.  For example, suppose the text in cell A1 is "Test."  Then the saved test file will contain """Test."""  How do I get it to save as a text file without enclosing double quote characters in quotes?
0
Comment
Question by:pittbb
8 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 11897454
You're going to have to use chr(34) in place of the double-quote:

xlFile.SaveAs(chr(34) & "test" & chr(34), outputfile)

Hope this helps.
-Jim
0
 
LVL 2

Author Comment

by:pittbb
ID: 11897579
The documentation on saveas is:

expression.SaveAs(FileName, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AddToMru, TextCodepage, TextVisualLayout, Local)

so the code you provided has "test" as the filename, and outputfile as the fileformat.  So let me clarify my question:

I'm not trying to do anything with the quotes in my code - the quotes are already parts of values in the cells of the worksheet I want to save.  When I save it (either through file > save as... on the menu, or through code), anywhere there is a " (i.e. a double quote) in any of the cells of the worksheet, it gets saved as """ (i.e. a double quote enclosed in quotes).  I want it to save as a text file without these extra quotes.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 11903267
I have used this trick in the past to save Excel data "as is" into a text file.

Run a piece of code, or a spreadsheet formula, to concatenate the entire row of data into a single cell.  Then save as a text file and check of Tab as your separator.

Leon
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Assisted Solution

by:craigewens
craigewens earned 62 total points
ID: 11904991
Alternatively.. the next line in VBA after you save the txt file would be to re-open it, then find & replace all the "" with ". You could use a function similar to this i'd have thought...

[code]
    xlFile.SaveAs filename:=outputFile, FileFormat:=xlText
    call ReplaceInFile(outputFile,chr(34) & chr(34),chr(34))
[/code]
Which would call...
[code]
Function ReplaceInFile(Filename As String, SearchVal As String, ReplaceVal As String)
    Dim BUFFER As String    
    BUFFER = Space(FileLen(Filename))
    Open Filename For Binary Access Read As #1
    Get #1, 1, BUFFER
    Close #1
    BUFFER = Replace(BUFFER, SearchVal, ReplaceVal)
    Kill Filename
    DoEvents
    Open Filename For Binary Access Read Write As #1
    Put #1, 1, BUFFER
    Close #1
End Function
[/code]

Does this help in any way?
0
 
LVL 2

Author Comment

by:pittbb
ID: 11906804
Leon,
    Can you elaborate a little.  You mean to contacenate everything in row 1 into cell A1, everything in row 2 into cell A2, etc, and then where (in code) do you have the option of setting tab as a separator?

Craigewens,
   I like your solution, except that it prompts to save the text file everytime.  Is there a way around this?
0
 
LVL 29

Accepted Solution

by:
leonstryker earned 63 total points
ID: 11906885
Actually, i just did sometests and I found that this works fairly well:

xlFile.SaveAs filename:=outputFile & ".txt", FileFormat:=xlTextPrinter

Leon
0
 
LVL 2

Author Comment

by:pittbb
ID: 11907125
Leon, that last one is what I was looking for.  Unfortunately, there's a sublety about it, so I'm not going to use it.  It seems to be putting spaces inbetween columns rather than tabs.  Both of you answered my question with a solution that worked for what I specified, so I think its fair that I split the points.  Here's what I think I'm going to use:

    iFileNum = FreeFile
    Open replace(outputFile, ".xls", ".txt") For Output As #iFileNum
    For i = 1 To xlSheet.Cells.SpecialCells(xlCellTypeLastCell).row
        For j = 1 To xlSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
            If Not IsError(xlSheet.Cells(i, j).Value) Then
                strRow = strRow & xlSheet.Cells(i, j).Value & Chr(9)
            End If
        Next j
        Print #iFileNum, strRow
        strRow = ""
    Next i
    Close #iFileNum
    xlFile.Close
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 11907262
As long as it works for you.  Thanks for the grade,

Leon
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now