Runtime Error 322: “Can't create necessary temporary file,” explained and fixed

Error 322, “Can't create necessary temporary file”, means an operation needed scratch space on disk and could not create its working file. The temp location comes from the TEMP environment setting, and the failure is one of three mundane things: the disk holding it is full, the account lacks write permission there, or the path TEMP points to no longer exists. It is an environment problem, and notably the program raising it is often a control or the host, not the macro itself.

For people who just run the program

  1. Check free space on the system drive first. Temp files live there by default, and a system drive at zero free space produces this error inside otherwise healthy programs.
  2. Restart the application, then the computer. Stale temp files from crashed sessions accumulate; a restart lets the system clear what it can and resets a stuck temp state.
  3. Check whether it follows your account or the machine. If a colleague signs in on the same PC and the tool works, your profile's temp folder permissions or quota are the problem, which is a specific, fixable thing to hand to IT.
  4. Report the drive and free space you found. For this error, “system drive had 40 MB free” is a complete and actionable report.

For people who write the code

  1. Surface the temp path in diagnostics. Log Environ$("TEMP") when this error is caught; a surprising share of cases are a TEMP variable pointing at a folder deleted by a cleanup tool or a drive that no longer exists.
  2. Test writability up front. Code that depends on scratch files can create and delete a probe file in the temp folder at start-up and report a precise message, instead of failing midway through real work.
  3. Dim probe As String, fn As Integer
    probe = Environ$("TEMP") & "\probe_" & Format$(Now, "hhnnss") & ".tmp"
    fn = FreeFile
    Open probe For Output As #fn   ' error 322's territory, caught early
    Close #fn
    Kill probe
  4. Fall back gracefully. Where the design allows, fall back to a document-adjacent working folder when the system temp is unusable, and say so in the log. Silent fallback hides a broken environment; logged fallback survives it.
Error 322 is often raised by a component, not by your code. Controls and the host application create temp files for printing, rendering, and undo. When it appears with no file code of yours anywhere nearby, check the environment before debugging the macro.

If it still happens

If space and permissions are fine, check the sheer count of files in the temp folder and any per-folder limits, and confirm the TEMP path is not a long-deleted mapped drive: an environment variable inherited from an old login script can point at a letter that no longer exists, which fails exactly like a permissions problem.

Common questions

Where is the temporary folder? Wherever the TEMP environment variable points, by default a Temp folder inside the user's local profile. Programs ask the system for the location rather than choosing it.

Can a full disk on a different drive cause error 322? Only if the temp location is on that drive. Temp files go to the TEMP drive, usually the system drive, which is why that drive's free space is the first thing to check.

Related errors

Runtime Error 321 · Runtime Error 61 · Runtime Error 71