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?

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

4 Comments

I tried this the next line fails LOF(lngFileNum) returns 0 and FileLen(lngFileNum) returns Run-Time Error '53' file not found
@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.
This is absolutely wrong. 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.
Tested fine here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.