I hope I can find a VBS code that can save the excel table as TXT. Although I can save it as a choice, I still need to directly run VBS to save it as TXT. How should I write the code?
If Excel is installed, it will be relatively simple. The following is a common tool that can run without Office, as shown below:
VBScript code:Copy codeThe Code is as follows: Set oShell = CreateObject ("Shell. Application ")
Set oDir = oShell. BrowseForFolder (0, "Select Directory", 0)
For Each x In oDir. Items
If LCase (Right (x. Path, 4) = ". xls" Then
XLS2TXT x. Path
End If
Next
'*************************************** **************************************** *********
'Start Conversion
'*************************************** **************************************** *********
Sub XLS2TXT (strFileName)
'If Excel is installed, you only need
'Oexcel. ActiveWorkbook. SaveAs strFileName & ". txt",-4158
'The following method is suitable for systems without Office Installation
On Error Resume Next
Dim oConn, oAdox, oRecordSet
Set oConn = CreateObject ("Adodb. Connection ")
Set oAdox = CreateObject ("Adox. Catalog ")
SConn = "Provider = Microsoft. Jet. Oledb.4.0 ;"&_
"Data Source =" & strFileName &";"&_
"Extended Properties =" "Excel 8.0; HDR = No "";"
SSQL = "Select * From"
OConn. Open sConn
If Err Then
Msgbox "error code:" & Err. Number & VbCrLf & Err. Description
Err. Clear
Else
OAdox. ActiveConnection = oConn
SSQL = sSQL & "[" & oAdox. Tables (0). Name & "]" 'For ease, only the first worksheet is processed
Set oRecordSet = oConn. Execute (sSQL)
If Err Then
Msgbox "error code:" & Err. Number & VbCrLf & Err. Description
Err. Clear
Else
Write strFileName & ". txt", oRecordSet. GetString
End if
End If
ORecordSet. Close
OConn. Close
Set oRecordSet = Nothing
Set oAdox = Nothing
Set oConn = Nothing
End Sub
'*************************************** **************************************** *********
'Write file, overwrite the same name, create if none
'*************************************** **************************************** *********
Sub Write (strName, str)
Dim oFSO, oFile
Set oFSO = CreateObject ("Scripting. FileSystemObject ")
Set oFile = oFSO. OpenTextFile (strName, 2, True) 'is created if it does not exist and is forcibly overwritten.
OFile. Write str
OFile. Close
Set oFile = Nothing
Set oFSO = Nothing
End Sub