Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

ms access - Working with Unicode file names in VBA (using Dir, FileSystemObject, etc.)

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.

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.

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.


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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

Filename: 1_English.txt
Contents: London is a city in England.

Filename: 2_French.txt
Contents: Paris is a city in France.

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

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:\__tmpso33685990files")
    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:\__tmpso33685990files1_English.txt
C:\__tmpso33685990files2_French.txt
C:\__tmpso33685990files3_Polish.txt

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:\__tmpso33685990files")
    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

... displaying

London is a city in England.
Paris is a city in France.
Warsaw is a city in Poland.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...