2

I am trying to open a file with a Unicode file name for binary access to calculate the MD5 check sum. I have the file names and path stored in a excel sheet correctly.

File Names Used:
File Names Used

The code then fails atOpen sPath For Binary Access Read As lngFileNumber with 'Run-Time error'52': Bad file name or number

Function GetFileBytes(ByVal sPath As String) As Byte()
    ' makes byte array from file
    Dim lngFileNum As Long, bytRtnVal() As Byte, bTest
    lngFileNum = FreeFile
    If LenB(Dir(sPath)) Then ''// Does file exist?
        Open sPath For Binary Access Read As lngFileNum
        'a zero length file content will give error 9 here
        ReDim bytRtnVal(0 To LOF(lngFileNum) - 1&) As Byte
        Get lngFileNum, , bytRtnVal
        Close lngFileNum
    Else
        Err.Raise 53 'File not found
    End If
    GetFileBytes = bytRtnVal
    Erase bytRtnVal
End Function

Any suggestions?

Community
  • 1
  • 1
MarkS
  • 23
  • 3

1 Answers1

2

You can make the filename "acceptable" by converting it with the StrConv function. I tried your code with this modification:

Open StrConv(sPath, vbUnicode) For Binary Access Read As #1

...and the Open command ran successfully with my test filename abc✓✘.mp3. I can't say for sure if it would works with yours since you included them as an image, but it should be okay.

Unrelated:
The next line (Redim...) has a different problem for you to debug. (Perhaps you can't use LOF with this type of file? or maybe use FileLen instead?.)


More Information:

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • I tried this the next line fails LOF(lngFileNum) returns 0 and FileLen(lngFileNum) returns Run-Time Error '53' file not found – MarkS Jun 01 '18 at 05:39
  • @MarkS - well, I mentioned you had more issues, but the one this question was about is solved. But, before you post a new question: did you see all the links that were provided? You need to read up on these functions yourself. At least one of them is being used incorrectly which would be obvious with a quick glance at the documentation. – ashleedawg Jun 01 '18 at 05:45
  • This is [absolutely wrong](https://stackoverflow.com/a/14292880/11683). The contents of the sheet is already in Unicode, as so is `sPath`. That the `Open` statement then cannot process that Unicode name is a completely different story, but it can't be made to do that with `StrConv(sPath, vbUnicode)` which results in "double Unicode" garbage. – GSerg Mar 04 '23 at 22:27
  • Tested fine here. – ashleedawg Jun 09 '23 at 22:34