Solved

VBA: Saving Excel as Text file

Posted on 2004-08-25
8
2,173 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…
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…

746 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

16 Experts available now in Live!

Get 1:1 Help Now