excelvbaspecial-charactersfile-copying

GetFile doesn't work with files which names contain of special characters


I have a VBA macro that loops through the files in a specified path, chooses the file with the newest date and writes the path of the file into a variable "FileToCopy". Unfortunately if the newest file contains any special characters (in my case it's a letter: "ż"), VBA reads it as "z". So a file "sprzedaż" becomes "sprzedaz". Then this line of code:

FileCopy FiletoCopy, Destination 

fails the macro, because there isn't a file in the system called "sprzedaz". So I get "File not found" error.

I tried to use CreateObject("Scripting.FileSystemObject").GetFile(FileToCopy) but I suppose it works the same way, because same error appears. I have also found another thread on stackoverlow with someone having the same problem and it was solved by using Chr function, but when I check the character list there is no "ż", which is the only letter I need: (Link_to_character_list). So I can't use this solution.

The entire code works perfect when ż is not there, so I really just a need to know how to copy a file with such character.


Solution

  • FSO can handle this - if I create a file with the same name "sprzedaż.xlsx" then this code:

    Dim fso As Object, f
    Set fso = CreateObject("scripting.filesystemobject")
    
    For Each f In fso.GetFolder("C:\Temp\").Files
        If f.Name Like "sprz*" Then
            [A1].Value = f.Name
            f.Copy "C:\Temp\sprzedaz2.xlsx"
            Exit For
        End If
    Next f
    

    ...put that name in A1 and created a copy.