VBS allows you to save an Excel file as a txt file.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.