Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA: Saving Excel as Text file

Posted on 2004-08-25
8
Medium Priority
?
2,403 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 66

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Assisted Solution

by:craigewens
craigewens earned 248 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 252 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 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