?
Solved

VBA: Saving Excel as Text file

Posted on 2004-08-25
8
Medium Priority
?
2,302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

752 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