Excel导出为Unicode管道分隔

时间:2012-10-12 15:07:09

标签: excel vba

现在我正在尝试将带有VBA的Excel工作表保存为带有.txt扩展名的管道分隔的unicode文件。

我已经想出如何将其保存为unicode,代码如下

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

但是这会将它保存为制表符分隔符。我似乎无法找到MSDN的选项,因为FileFormat上的页面不是很有帮助。

2 个答案:

答案 0 :(得分:0)

由于.csv的保存不能保持unicode,你可以尝试用文本文件中的管道替换选项卡,使用像这样的宏,找到here。你可以用

来调用它
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

答案 1 :(得分:0)

我还应该指出,搜索和替换不会正确修复引号分隔符。