我迭代通过文件夹中的文件(这意味着我不知道文件夹中的名称),并且有一个带有波兰语ł
字符的文件。
Dir
函数将其转换为l
,这意味着以后无法找到文件名。我已经声明了var,我将dir值指定为字符串。
我也尝试了FSO和getfolder,它也有同样的问题。
我也注意到文件对话框(设置为文件夹选择模式)也会转换上面的字符。
这是一个错误,还是可以解决的问题?
答案 0 :(得分:6)
虽然 VBA本身支持Unicode字符,但听起来好像被误导了, VBA开发环境却没有。 VBA编辑器仍然使用基于Windows中区域设置的旧“代码页”字符编码。
当然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
...在立即窗口中生成以下输出...
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.