使用 VBA 中的 Unicode 文件名(使用 Dir、FileSystemObject 等)
Working with Unicode file names in VBA (using Dir, FileSystemObject, etc.)
我正在迭代 浏览文件夹中的文件(这意味着我不知道文件夹中的名称),并且有一个带有波兰语 ł
字符的文件.
Dir
函数将其转换为 l
,这意味着以后无法找到文件名。我已将要为其分配 dir 值的 var 声明为字符串。
我也试过 FSO 和 getfolder 也有同样的问题。
我还注意到文件对话框(设置为文件夹 select 模式)也会转换上面的字符。
这是一个错误,还是可以解决的问题?
听起来您被以下事实误导了,虽然 VBA 本身 支持 Unicode 字符,但 VBA 开发环境 没有。 VBA 编辑器仍然根据 Windows.
中的区域设置使用旧的 "code page" 字符编码
当然 FileSystemObject
等。 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
... 立即生成以下输出 window ...
C:\__tmp\so33685990\files_English.txt
C:\__tmp\so33685990\files_French.txt
C:\__tmp\so33685990\files_Polish.txt
注意Debug.Print
语句将ł
字符转换为l
,因为VBA开发环境无法使用我的[=55=显示ł
] 语言环境(美国英语)。
但是,以下代码确实成功打开了所有三个文件...
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.
我正在迭代 浏览文件夹中的文件(这意味着我不知道文件夹中的名称),并且有一个带有波兰语 ł
字符的文件.
Dir
函数将其转换为 l
,这意味着以后无法找到文件名。我已将要为其分配 dir 值的 var 声明为字符串。
我也试过 FSO 和 getfolder 也有同样的问题。
我还注意到文件对话框(设置为文件夹 select 模式)也会转换上面的字符。
这是一个错误,还是可以解决的问题?
听起来您被以下事实误导了,虽然 VBA 本身 支持 Unicode 字符,但 VBA 开发环境 没有。 VBA 编辑器仍然根据 Windows.
中的区域设置使用旧的 "code page" 字符编码当然 FileSystemObject
等。 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
... 立即生成以下输出 window ...
C:\__tmp\so33685990\files_English.txt
C:\__tmp\so33685990\files_French.txt
C:\__tmp\so33685990\files_Polish.txt
注意Debug.Print
语句将ł
字符转换为l
,因为VBA开发环境无法使用我的[=55=显示ł
] 语言环境(美国英语)。
但是,以下代码确实成功打开了所有三个文件...
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.