Frage

This is a question related to: Create text Files from every row in an Excel spreadsheet I have implemented ExactaBox great solution with the following code:

Sub SaveRowsAsENW()

Dim wb As Excel.Workbook, wbNew As Excel.Workbook
Dim wsSource As Excel.Worksheet, wsTemp As Excel.Worksheet
Dim r As Long, c As Long

    Set wsSource = ThisWorkbook.Worksheets("worksheet1")

    Application.DisplayAlerts = False 'will overwrite existing files without asking

    r = 1
    Do Until Len(Trim(wsSource.Cells(r, 1).Value)) = 0
        ThisWorkbook.Worksheets.Add ThisWorkbook.Worksheets(1)
        Set wsTemp = ThisWorkbook.Worksheets(1)

        For c = 2 To 7
            wsTemp.Cells((c - 1) * 2 - 1, 1).Value = wsSource.Cells(r, c).Value
        Next c

        wsTemp.Move
        Set wbNew = ActiveWorkbook
        Set wsTemp = wbNew.Worksheets(1)
        'wbNew.SaveAs wsSource.Cells(r, 1).Value & ".csv", xlCSV 'old way
        wbNew.SaveAs "textfile" & r & ".enw", xlCSV 'new way
        'you can try other file formats listed at http://msdn.microsoft.com/en-us/library/office/aa194915(v=office.10).aspx
        wbNew.Close
        ThisWorkbook.Activate
        r = r + 1
    Loop

    Application.DisplayAlerts = True

End Sub
Option Explicit

I have used this solution and it works fine. The only trouble I have is that some of the lines get quotation marks in the output file.

This is an example of output text file (line 2-3 demonstrates the error):

0  Journal Article 'No quotation marks
"%A  Wofford, J.C."
"%A  Goodwin, Vicki L."
%T  A field study of a cognitive approach to understanding transformational and .. 'No quotation marks

This formatting seem to be added when it is being saved (it is not part of the cell formatting). Do any of you have any ideas of why this happens? /How can I adapt my code to fix it?

War es hilfreich?

Lösung

True. .csv stands for comma-separated values, where a field contains a comma it has to be 'escaped' (here with quotes) or would be split into different fields before/after each comma. Answer provided before does however offer alternatives - of which Tab delimited is the most logical.

Andere Tipps

This is likely past the point of being helpful to you, but after hitting this problem recently myself I thought I'd share my eventual solution. The formatting you're seeing is actually the result of a MS saving issue, which appends quotes to lines that have certain characters.

In my case I wrote out the file as usual and then called a sub that cleans the file of the problem extra characters. First I replaced any output that would need quotes with something like an asterisk or any other character that would never occur in my file. Then I saved the file as normal and called the below code, used to replace any character with another, twice. Once to remove the quotes Excel created, the second time to replace my dummy character with quotes. The code executes fairly quickly and renames the file so you can be certain the result is finished processing. Hopefully useful to others searching.

It's still clunkier than I'd like since you save a file and then edit it, but it worked well enough to become my final solution in the end.

    Sub ReplaceStringInTextFile(FileNameAndLoc As String, OutFile As String, SearchForWords As String, SubstituteWords As String)
        'This macro searches a file, replacing one string with another, saving it, and renaming it.
        Dim objFSO As Object
        Dim objReadFile As Object
        Dim objWriteFile As Object

        'Set Objects
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objReadFile = objFSO.opentextfile(FileNameAndLoc, 1, False)

        'Read file contents
        Contents = objReadFile.readall

        'Close read file
        objReadFile.Close

        'Copy contents without double quotes
        NewContents = Replace(Contents, SearchForWords, SubstituteWords)

        'Write output
        Set objWriteFile = objFSO.opentextfile(FileNameAndLoc, 2, False)
        objWriteFile.write NewContents
        objWriteFile.Close

        'Rename file
        Name FileNameAndLoc As OutFile
    End Sub
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top