GCuser99 / SeleniumVBA

A comprehensive Selenium wrapper for browser automation developed for MS Office VBA running in Windows
MIT License
89 stars 18 forks source link

VBA native file system functions fail with Unicode directory and file paths #79

Closed GCuser99 closed 1 year ago

GCuser99 commented 1 year ago

I've been working on SeleniumVBA functionality for proper handling of Unicode directory and file paths. In the process, I came across the following surprise (to me).

Apparently on my system, native VBA7 functionality (Excel 365) blows up when trying to access/manipulate file names and paths that contain non-ANSI characters. In VBA, the following code never makes it past the MKDir command at runtime, and will also fail on Open, Kill, Dir, and RmDir commands.

I naively thought that VBA strings, represented as Unicode, would work with native file commands, but apparently not.

Can anyone else confirm that the following code fails in VBA? Thx!

PS: FileSystemObject and ADODB stream properly handle Unicode directory and file paths - so it is not too high an expectation for native VBA functionality to do the same, I think. But wanted to make sure that it is not something unique to my setup.


Sub test_unicode()
    Dim folderPath As String
    Dim filePath As String
    Dim fileNum As Long

    'create a folder with non-Ansi (unicode) characters

    folderPath = Environ("USERPROFILE") & "\Documents\" _
       & ChrW(&H259) & ChrW(&H259) & ChrW(&H259)

    MkDir folderPath

    'create a file with non-Ansi (unicode) characters
    'in the file name

    filePath = folderPath & "\" & "test" _
       & ChrW(&H278) & ".txt"

    fileNum = FreeFile
    Open filePath For Output As #fileNum
    Print #fileNum, "test line"
    Close #fileNum

    'check if success
    Debug.Print Dir(filePath) <> vbNullString

    'now clean up
    Kill filePath
    RmDir folderPath
End Sub
GHRyunosuke commented 1 year ago

Yes, the above code fails. Tested with my system (office excel 365 Japanese Version + Windows 11 Japanese Version)

image

GCuser99 commented 1 year ago

Thanks for confirming @GHRyunosuke. With the latest release, I'm going to close this one....