将xlsx转换为txt制表符分隔符 - 批处理过程

时间:2017-02-13 16:49:07

标签: excel-vba vbscript

我试图将xlsx文件转换为制表符分隔的文本文件,但文本文件中的输出看起来是随机文本。下面是我的源代码。任何有关将其作为制表符分隔的txt文件输出的帮助将非常感激。我使用了一个带有两列的常规工作簿,带有标题,但无法转换为制表符分隔的txt文件。

Option Explicit

Dim oFSO, myFolder
Dim xlText

myFolder="C:\Projects\scripts\test"


Set oFSO = CreateObject("Scripting.FileSystemObject")
xlText = -4158 'Excel txt format enum
Call ConvertAllExcelFiles(myFolder)
Set oFSO = Nothing

Call MsgBox ("Done!")

Sub ConvertAllExcelFiles(ByVal oFolder)
    Dim targetF, oFileList, oFile
    Dim oExcel, oWB, oWSH

    Set oExcel = CreateObject("Excel.Application")
    oExcel.DisplayAlerts = False
    Set targetF = oFSO.GetFolder(oFolder)
    Set oFileList = targetF.Files
    For Each oFile in oFileList
        If (Right(oFile.Name, 4) = "xlsx") Then
            Set oWB = oExcel.Workbooks.Open(oFile.Path)
            For Each oWSH in oWB.Sheets
                Call oWSH.SaveAs (oFile.Path & ".txt")
            Next
            Set oWSH = Nothing
            Call oWB.Close
            Set oWB = Nothing
        End If
    Next
    Call oExcel.Quit
    Set oExcel = Nothing

End Sub

1 个答案:

答案 0 :(得分:-1)

您在SaveAs上缺少FileFormat参数,因此它实际上并没有将Excel文件转换为Text,只是添加了扩展名。

尝试更改

Call oWSH.SaveAs (oFile.Path & ".txt")

Call oWSH.SaveAs (oFile.Path & ".txt", FileFormat:=xlTextWindows )