Solved

VBA: Saving Excel as Text file

Posted on 2004-08-25
8
2,182 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

919 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

19 Experts available now in Live!

Get 1:1 Help Now