從單元格中檢索的值已經是Unicode。
StrConv(vbUnicode)
給你「雙unicode」,因爲它通過使用當前sustem代碼頁進行轉換而被破壞。
然後,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
和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
你不必非得來設置基準,(雖然我不知道這是爲什麼我們做這樣的問題)。你可以使用沒有引用的後期綁定: - Dim FSO作爲Object {newline}設置FSO = CreateObject(「Scripting.FileSystemObject」) - 但你失去了所有的Intellisense和預定義的常量。你確定這種方法有幫助嗎?我仍然收到錯誤的文件名錯誤。 – Morbo