从单元格中检索到的值已经是 Unicode。
StrConv(vbUnicode)
为您提供“双 unicode”,该代码已损坏,因为它使用当前系统代码页进行了转换。
然后该Print
命令将其转换回“单一 unicode”,再次使用当前系统代码页。不要这样做。您不是在保存 unicode,而是在保存无效的东西,这些东西可能只在您当前设置下的特定计算机上显示有效。
如果要输出 Unicode 数据(即避免默认的 VB 将输出文本从 Unicode 自动转换为 ANSI 的机制),您有多种选择。
最简单的方法是在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
和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