Create file with Cyrillic characters in filename in macOS with VBA

198 views Asked by At

I want to create a text file with a filename that has some Cyrillic characters.

This MS Word VBA code does not work since I moved from Windows to Mac:

Dim fname As String
fname = db_path + "кириллица-test123.txt"
Close #1

Open fname For Output As #1
Print #1, "Hello!"
Print #1, "Привет!"
Close #1

I get

Run-time error '75': "Path/File access error".

It works if I say:

frame = db_path + "test.123.txt"

I learned File System Objects are only supported on Windows.

I am on macOS (default language = Russian). I do not know any way to create a text file other than the "Open" statement.

I tried applying StrConv function to the filename.

1

There are 1 answers

0
jonsson On

Can't provide a solution to this right now, but...

The main problem here seems to be that on Mac, the old built-in VBA File commands only recognise 8 bit character codes.

What I can't tell without a lot of reconfiguration of my Mac is exactly how that affects someone who has a Mac set up for a Cyrillic script language.

Here (on an English language set-up) it affects what I see in the VB Editor (i.e. all the Cyrillic characters appear as "?"). That's expected.

But let's say I change your file name to a single Cyrillic-Њ character using this:

fname = db_path +  Chr(&H400)  & "-test123.txt"

I get an "invalid procedure call or argument" error.

But if I use the 2-byte UTF8 equivalent of &H400, like this...

fname = db_path + Chr(208) & Chr(138) & "-test124.txt"

...a file is created with a name starting with the &H400 character as you might hope.

further, using Chr(208) & Chr(138) to write the same character to the file seems to work.

No solid ground for a solution there, but perhaps something to explore as I am away for a few days now.