1

ワークブックの内容を txt ファイルに保存するためのボタン クリックの下に記述されたコードがあります。

コード:

Private Sub CommandButton1_Click()
    Dim xlApp As Object
    Dim xlBook As Object
    Dim xlSheet As Object
    Dim strOutputFileName

    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Open("C:\Documents and Settings\bera02a\Desktop\Arun_TAT_Testing_orig_14022013.xls")

    For Each xlSheet In xlBook.Worksheets
        strOutputFileName = "C:\Documents and Settings\bera02a\Desktop\" & xlSheet.Name & ".txt"
        xlSheet.SaveAs Filename:=strOutputFileName, FileFormat:=xlUnicodeText
    Next
    xlApp.Quit
End Sub

ワークブックの元の行:

create table abc as select rpt2_id, rpt2_usgcount, rpt2_usgdate, rpt2_usgmins from rpt2 where rpt2_id = 'xyz;

出力は次のようになります。

"create table abc as select rpt2_id, rpt2_usgcount, rpt2_usgdate, rpt2_usgmins from rpt2 where rpt2_id = 'xyz;"

出力は開始と終了に二重引用符が付いており、ワークブックに存在する挿入および作成ステートメントに対してのみ送信されます。

出力にその引用符が表示されないように助けてくれる人はいますか?

4

1 に答える 1

1

あなたは特にピーターの助けを求めましたが、これがあなたが望むものかどうか見てみましょう?

xlSheet.SaveAs Filename:=strOutputFileName, FileFormat:=xlTextPrinter

フォローアップ (コメントから)

これを試して

Private Sub CommandButton1_Click()
    Dim xlBook As Workbook, xlSheet As Worksheet
    Dim strOutputFileName As String
    Dim n As Long, i As Long, j As Long
    Dim MyData As String, strData() As String, MyArray() As String
    Dim strPath As String

    strPath = ActiveWorkbook.Path '<~~ \\plyalnppd3sm\d$\Temp\Arun\TAT\

    ThisWorkbook.SaveCopyAs strPath & "\Temp.xls"

    Set xlBook = Workbooks.Open(strPath & "\Temp.xls")

    For Each xlSheet In xlBook.Worksheets
        If xlSheet.Name <> "User_provided_data" Then
            strOutputFileName = strPath & "\" & xlSheet.Name & ".zup"
            xlSheet.SaveAs Filename:=strOutputFileName, FileFormat:=xlTextMSDOS

            n = n + 1

            ReDim Preserve MyArray(n)
            MyArray(n) = strOutputFileName
            Debug.Print strOutputFileName
        End If
    Next

    xlBook.Close SaveChanges:=False

    Kill strPath & "\Temp.xls"

    For i = 1 To UBound(MyArray)
        '~~> open the files in One go and store them in an array
        Open MyArray(i) For Binary As #1
        MyData = Space$(LOF(1))
        Get #1, , MyData
        Close #1
        strData() = Split(MyData, vbCrLf)

        '~~> Write to the text file
        Open MyArray(i) For Output As #1

        '~~> Loop through the array and check if the start and end has "
        '~~> And if it does then ignore those and write to the text file
        For j = LBound(strData) To UBound(strData)
            If Left(strData(j), 1) = """" And Right(strData(j), 1) = """" Then
                strData(j) = Mid(strData(j), 2, Len(strData(j)) - 2)
            End If
            Print #1, strData(j)
        Next j
        Close #1
    Next i
End Sub
于 2013-02-18T16:23:16.253 に答える