简体   繁体   中英

Excel Export as Unicode Pipe Delimited

Right now I'm trying to save an excel sheet with VBA as a pipe delimited unicode file with a .txt extension.

I've figured out how to save it as unicode with code as follows

ActiveWorkbook.SaveAs FileName:=FileName, _
FileFormat:=xlUnicodeText

But this will save it as tab delimited. I can't seem to find an option with MSDN, as their page on FileFormat isn't very helpful.

As saving as a .csv doesn't keep it unicode, you can try replacing the tabs by pipes in the text file, using a macro like this one, found here . You can call it using

call TextFileReplace("C:\temp\test.txt","C:\temp\test2.txt",vbtab,"|")

Public Sub TextFileReplace(ByVal sFile As String, ByVal sNewFile As String, ByVal sFind As String, ByVal sReplace As String)
Dim iFile As Integer
Dim sTextBuffer As String
'
' Get the next available file handle
iFile = FreeFile
' Open the source file (sFile) for read access
Open sFile For Binary Access Read As iFile
' Create a buffer that will hold the contents of the file
sTextBuffer = Space(LOF(iFile))
' Read the contents of the file into the buffer
Get #iFile, , sTextBuffer
' Close the file
Close iFile

' Use the "Replace" function to replace all instances of
' (sFind) in the buffer with the value in (sReplace)
sTextBuffer = Replace(sTextBuffer, sFind, sReplace)

' Get the next available file handle
iFile = FreeFile
' Open/Create the new file for write access
Open sNewFile For Binary Access Write As iFile
' Write the modified buffer contents to the file
Put #iFile, , sTextBuffer
' Close the file
Close iFile
End Sub

我还应该指出,search-n-replace无法正确修复带引号的定界符。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM