在VBA中使用Unicode文件名(使用Dir,FileSystemObject等) [英] Working with Unicode file names in VBA (using Dir, FileSystemObject, etc.)

查看:447
本文介绍了在VBA中使用Unicode文件名(使用Dir,FileSystemObject等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过文件夹中的文件(这意味着我不知道文件夹中的名称)迭代,并具有波兰语ł字符。

I am iterating through files in a folder (which means I do not know the names in the folder), and have a file with a Polish ł character.

Dir 函数将其转换为 l 意味着以后无法找到文件名。我已经声明了我将dir值分配给一个字符串。

The Dir function converts this to an l which means the filename can't be found at a later date. I've declared the var that I'm assigning the dir value to as a string.

我也试过FSO和getfolder也有同样的问题。

I've also tried FSO and getfolder which also has the same issue.

我也注意到文件对话框(设置为文件夹选择模式)也转换上面的字符。

I've also noticed the file dialog (set to folder select mode) converts the character above too.

这是一个bug,还是可以解决的问题?

Is this a bug, or is it something that can be worked around?

推荐答案

p>这听起来像是被VBA本身支持Unicode字符的事实所误导,但是VBA开发环境不支持。 VBA编辑器仍然使用基于Windows中的区域设置的旧的代码页字符编码。

It sounds like you are being misled by the fact that while VBA itself supports Unicode characters, the VBA development environment does not. The VBA editor still uses the old "code page" character encodings based on the locale setting in Windows.

当然 FileSystemObject 等。实际上,它支持文件名中的Unicode字符,如以下示例所示。包含三个纯文本文件的文件夹

Certainly FileSystemObject et. al. do in fact support Unicode characters in file names, as illustrated by the following example. With a folder containing three plain text files

文件名: 1_English.txt

内容: 伦敦是英国的一个城市。

文件名: 2_French.txt

内容:巴黎是法国的一个城市。

文件名: code>3_Połish.txt

内容:华沙是波兰的一个城市。

Filename: 3_Połish.txt
Contents: Warsaw is a city in Poland.

以下VBA代码...

The following VBA code ...

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

...生成在立即窗口中输出以下输出...

... produces the following output in the Immediate window ...

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语言环境(美国英语)显示ł

Note that the Debug.Print statement converts the ł character to l because the VBA development environment cannot display ł using my Windows locale (US English).

但是,以下代码确实打开了所有三个文件...

However, the following code does open all three files successfully ...

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.

这篇关于在VBA中使用Unicode文件名(使用Dir,FileSystemObject等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆