听起来您被以下事实误导了:VBA本身支持 Unicode 字符,VBA开发环境才不是。 VBA 编辑器仍然使用基于 Windows 中区域设置的旧“代码页”字符编码。
当然FileSystemObject
et. al.事实上,确实支持文件名中的 Unicode 字符,如以下示例所示。包含三个纯文本文件的文件夹
文件名:1_English.txt
内容:London is a city in England.
文件名:2_French.txt
内容:Paris is a city in France.
文件名:3_Połish.txt
内容:Warsaw is a city in Poland.
以下VBA代码...
Option Compare Database
Option Explicit
Sub scanFiles()
Dim fso As New FileSystemObject, fldr As Folder, f As File
Set fldr = fso.GetFolder("C:\__tmp\so33685990\files")
For Each f In fldr.Files
Debug.Print f.Path
Next
Set f = Nothing
Set fldr = Nothing
Set fso = Nothing
End Sub
...在立即窗口中产生以下输出...
C:\__tmp\so33685990\files\1_English.txt
C:\__tmp\so33685990\files\2_French.txt
C:\__tmp\so33685990\files\3_Polish.txt
请注意,Debug.Print
语句将转换为ł
字符到l
因为VBA开发环境无法显示ł
使用我的 Windows 区域设置(美国英语)。
但是,以下代码确实成功打开了所有三个文件......
Option Compare Database
Option Explicit
Sub scanFiles()
Dim fso As New FileSystemObject, fldr As Folder, f As File, ts As TextStream
Set fldr = fso.GetFolder("C:\__tmp\so33685990\files")
For Each f In fldr.Files
Set ts = fso.OpenTextFile(f.Path)
Debug.Print ts.ReadAll
ts.Close
Set ts = Nothing
Next
Set f = Nothing
Set fldr = Nothing
Set fso = Nothing
End Sub
... 显示
London is a city in England.
Paris is a city in France.
Warsaw is a city in Poland.