Runtime Error 321: “Invalid file format,” explained and fixed
Error 321, “Invalid file format”, means a file was opened or loaded successfully but its contents are not what the operation requires. The file exists and is readable; its internal structure is wrong for the job, a text file where a binary form file is expected, a renamed image that is not really that format, a resource or data file from an incompatible version. It is a content mismatch, and the first diagnostic question is always: what actually is this file, regardless of its name?
For people who just run the program
- Check the file's real type, not its extension. Files get renamed constantly: a spreadsheet saved as .csv that is really an .xlsx, a picture that is actually a different format. Open it in its proper application and use Save As to produce a genuine copy in the needed format.
- Get a fresh copy from the source. A file truncated by a failed download keeps a plausible name and size while its internal structure is incomplete. Re-download or re-export rather than repairing by hand.
- Note whether the file came from a newer version of a program. Older tools reading newer formats is the standard version of this error; the fix is saving in the older format or updating the tool.
- Report where the file came from. “Exported from the new system” versus “emailed by a colleague” leads to different fixes.
For people who write the code
- Validate before loading. Check the file's signature (the first bytes) against the format expected: form and resource files, images, and database files all have recognizable headers. A five-line check turns error 321 into a message that names the actual format found.
- Distinguish your Load errors. When code loads several file kinds (forms, resources, pictures, data), log which load raised the error. Error 321's text is identical for all of them; the context is the entire diagnosis.
Dim pic As StdPicture
On Error GoTo BadPic
Set pic = LoadPicture(path) ' error 321 if the bytes are not a picture
Exit Sub
BadPic:
MsgBox path & " is not a usable picture file."
- Version-stamp files your program writes. A header line or field with a format version lets future reads reject incompatible files deliberately, with a migration path, instead of failing inside a loader.
Renaming a file never converts it. Half of all error 321 reports trace to a file whose extension was edited to match what a program asked for. The bytes decide the format; only a real conversion (open, Save As the target type) changes them.
If it still happens
If the file is verifiably the right format and a fresh copy also fails, suspect the reading side: a control or library older than the file's sub-version. Compare the version that wrote the file with the version of the component loading it; a point release gap in either direction is enough.
Common questions
How is error 321 different from error 53, file not found? Error 53 means the file could not be located or opened at all. Error 321 means it opened, but its contents do not match the format the operation requires.
Can a corrupt file cause error 321? Yes. Corruption that damages the header or internal structure makes a file the wrong format in effect, even if it started life correctly. A fresh copy from the source is the cleanest test.
Related errors