Runtime Error 52: “Bad file name or number,” explained and fixed

Error 52, “Bad file name or number”, covers two faults that share one message: a file name the system will not accept (empty, containing illegal characters, or an invalid path form), and a file number used with statements like Print # or Close that does not refer to an open file. Both are code bugs, usually in how a name is built from data or how a file number is tracked.

For people who just run the program

  1. Look at the name the data produced. If the macro names files after records, a customer name with a slash, a date with slashes, or a blank name will produce an illegal file name and this error.
  2. Check for empty or odd entries in the source data. The record being processed when it failed is the first suspect; names built from empty fields are a classic trigger.
  3. Try one record with a plain name. If a simple record processes cleanly, the code is fine and one data value is poisoning the file name.
  4. Report the failing record's name field as it appears. The maintainer needs the raw value, including any punctuation, to reproduce it.

For people who write the code

  1. Sanitize names built from data. Replace the characters Windows forbids in file names (\ / : * ? " < > |) and trim trailing spaces and dots before composing the path:
  2. Function SafeName(s As String) As String
        Dim bad As Variant, i As Long
        bad = Array("\", "/", ":", "*", "?", Chr$(34), "<", ">", "|")
        For i = LBound(bad) To UBound(bad)
            s = Replace(s, CStr(bad(i)), "-")
        Next i
        SafeName = Trim$(s)
    End Function
  3. Always use FreeFile for file numbers. Hard-coded numbers collide with files opened elsewhere in the project. Dim fn As Integer: fn = FreeFile guarantees the number is genuinely free, and every later statement should use that variable.
  4. Close in the same procedure that opens. A file number stored in a global and closed in two places will eventually be used after close. Pair Open and Close, and route errors through a handler that closes only if the open succeeded.
An empty string file name raises error 52, not file not found. When a name is assembled from fields, test the assembled name for emptiness before the Open; the error message misleads people into hunting through folders for a file that was never named at all.

If it still happens

If names are legal and numbers come from FreeFile, check whether the path is being interpreted differently than expected: a relative path resolves against the application's current directory, which other code (or a common dialog) may have changed. Print the full resolved path at the point of failure.

Common questions

Which characters are illegal in a Windows file name? Backslash, slash, colon, asterisk, question mark, double quote, angle brackets, and vertical bar cannot appear in a file name. A name built from data containing any of them raises error 52 when used.

What does the “number” half of error 52 mean? VBA file statements refer to open files by number. Using a number that was never opened, or was already closed, is a bad file number and raises the same error 52.

Related errors

Runtime Error 53 · Runtime Error 54 · Runtime Error 63