我有一个将 excel 文件转换为文本文件的 Visual Basic 脚本。假设我有一个名为 example.xlsx 的 excel 文件;目前,脚本将其保存为 example.xlsx.txt,这不是我想要的。我需要它保存为:example.txt
有任何想法吗?
Option Explicit
Dim oFSO, myFolder
Dim xlTXT
myFolder="C:\..."
Set oFSO = CreateObject("Scripting.FileSystemObject")
xlTXT = 21 '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", xlTXT )
Exit For
Next
Set oWSH = Nothing
Call oWB.Close
Set oWB = Nothing
End If
Next
Call oExcel.Quit
Set oExcel = Nothing
End Sub