Working with Unicode file names in VBA (using Dir, FileSystemObject, etc.) Working with Unicode file names in VBA (using Dir, FileSystemObject, etc.) vba vba

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


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 DatabaseOption ExplicitSub 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 = NothingEnd Sub

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

C:\__tmp\so33685990\files\1_English.txtC:\__tmp\so33685990\files\2_French.txtC:\__tmp\so33685990\files\3_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 DatabaseOption ExplicitSub 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 = NothingEnd Sub

... displaying

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