0

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

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

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

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

4

2 回答 2

0

我还应该指出,search-n-replace 不会正确修复带引号的分隔符。

于 2013-02-07T20:21:01.687 回答
0

由于保存为 .csv 不会使其保持 unicode,因此您可以尝试使用像这样的宏在文本文件中用管道替换选项卡,在此处找到。您可以使用它来调用它

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
于 2012-10-12T15:59:12.543 回答