从单元格检索的值已采用 Unicode 格式。
StrConv(vbUnicode)
给你“双unicode”,它被破坏了,因为它使用当前系统代码页进行了转换。
然后Print
命令将其转换回“单一 unicode”,再次使用当前系统代码页。不要这样做。您没有保存 unicode,而是保存了无效的内容,这些内容可能仅在当前设置下的特定计算机上显示为有效。
如果您想要输出 Unicode 数据(即,避免将输出文本从 Unicode 自动转换为 ANSI 的默认 VB 机制),您有多种选择。
最简单的是使用FileSystemObject
无需尝试发明任何有关 unicode 转换的内容:
With CreateObject("Scripting.FileSystemObject")
With .CreateTextFile("C:\" & Cells(1).Value & ".txt", , True)
.Write Cells(1).Value
.Close
End With
End With
请注意控制 Unicode 的最后一个参数。
如果你不想这样,你可以声明CreateFileW
and WriteFile
功能:
Private Declare Function CreateFileW Lib "kernel32.dll" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByRef lpOverlapped As Any) As Long
Private Const CREATE_ALWAYS As Long = 2
Private Const GENERIC_WRITE As Long = &H40000000
Dim hFile As Long
hFile = CreateFileW(StrPtr("C:\" & Cells(1).Value & ".txt"), GENERIC_WRITE, 0, ByVal 0&, CREATE_ALWAYS, 0, 0)
Dim val As String
val = Cells(1).Value
WriteFile hFile, &HFEFF, 2, 0, ByVal 0& 'Unicode byte order mark (not required, but to please Notepad)
WriteFile hFile, ByVal StrPtr(val), Len(val) * 2, 0, ByVal 0&
CloseHandle hFile