This error occurs when Visual Basic for Applications cannot write temporary files to the system’s temporary folder. The fix is to change the temporary file path in your VBA project to a location where the user has write permissions, or to manually grant the user Full Control rights to the existing temporary folder.
What You'll Need
- Access to the VBA editor (Alt + F11).
- Administrator privileges on the Windows machine where the code is running.
- A location on the local drive with write permissions (e.g., a folder on the C drive or a mapped network drive).
Step-by-Step
- Open the VBA Editor. Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window. If your code is inside an Excel workbook, you can also right-click the sheet name in the project explorer, select View Code, and press F5 to run it from there.
- Identify the specific line causing the crash. Look at the Debug window (if it appears) or the Immediate Window (Ctrl + G). The error message usually points to the specific line of code attempting to save a file to the temp directory. Common culprits include the
SaveCopyAsmethod or using theFileCopyfunction with a path ending in"\Temp\"orEnviron("TEMP"). - Check the immediate cause. The runtime error 735 specifically means the code tried to save a file to a location that is not writable. This is almost always due to Windows User Account Control (UAC) restrictions or the file path being invalid. The user account running the VBA script (often the logged-in Windows user) does not have permission to write to the system's temporary folder.
- Open the VBA Project properties. In the VBA Editor, press the keyboard shortcut Ctrl + R to ensure the Project Explorer is visible. In the Project Explorer window, look for your project name (usually at the very top, often labeled something like "VBAProject" or "Book1 - VBAProject"). Right-click this top-level node and select Properties.
- Change the Project Name for debugging. In the Properties window that opens, look for the (Name) property. While this is often used to rename the code module, changing it here is a quick way to ensure the project is not locked or corrupted, though it does not fix the permissions issue directly. However, this step is often skipped in favor of the direct path fix.
- Locate the file path in your code. Scroll through your code until you find the string of text defining the temporary directory. It will likely look like a string variable or a hard-coded path.
- Hard-code a safe alternative path. Instead of using the system environment variable (which points to a protected system folder), change the path in your code to point to a folder on the C: drive where the user definitely has write permissions. For example, change a line like
Path = Environ("TEMP")toPath = "C:\Temp\"orPath = "C:\Users\" & Application.UserName & "\Desktop\Temp\". Ensure the folder actually exists; if not, use the MkDir command to create it before writing the file. - Verify the new path syntax. Ensure that backslashes are used correctly. If you are using a variable, check that you are not missing a backslash at the end, which would cause the filename to be appended directly to the folder name without a separator.
- Run the code again. Press F5 to execute the subroutine. If the error is resolved, the file will now be saved to the new, writable location you specified.
Common Mistakes
- Assuming the code is buggy. A common reaction is to assume the VBA code is flawed. However, VBA runtime error 735 is almost exclusively an operating system permission issue, not a code logic error. The code is doing exactly what it is told: trying to save a file to a specific directory.
- Ignoring the Immediate Window. The Immediate Window (Ctrl + G) is essential for debugging. It tells you exactly which line of code failed. If you only see a generic error box, you waste time fixing code that works perfectly fine but is being blocked by the OS.
- Using UNC paths without verification. If you are working in a network environment, you might try to save to a network share. If that share is mapped as a drive letter, it works. If you try to use a Universal Naming Convention (UNC) path like
"\\Server\Share\Temp"and the user lacks permissions on that specific share, the error will persist. - Forgetting to create the directory. If you change the temp path to a new folder on the C drive, that folder must exist. If you do not create it first (using
MkDir "C:\MyNewTempFolder"), the VBA script will fail with a different error before it even gets to runtime error 735.
Tips
- Always check the user's identity. In a corporate environment, a user might be running a macro as a standard user while the script is trying to write to a system folder. Running the Excel/VBA application as an administrator (right-click Excel > Run as administrator) will often bypass this specific error, though it may trigger other UAC prompts.
- Use the Application.PathSeparator. When constructing file paths programmatically, use the
Application.PathSeparatorconstant instead of hard-coding a backslash. This ensures the code works correctly on both Windows (which uses backslashes) and Mac (which uses forward slashes). - Check for file locks. Even if you have write permissions, another process might be holding the file open. If you are attempting to overwrite a file in the temp directory that a system cleaner or another instance of the application is currently using, you may encounter a permission denial.
- Disable macros temporarily for testing. If you are troubleshooting a module received from someone else, disable macros immediately after saving it. This prevents accidental macro execution that might modify system settings or create unwanted files while you are trying to fix the permissions issue.
Troubleshooting
If you have changed the path and the error persists, or if you cannot access the VBA editor due to the error, proceed with these checks:
- Verify the folder permissions manually. Navigate to the folder you are trying to write to in File Explorer. Right-click the folder > Properties > Security. Ensure the user account running the script has at least "Read & execute" and "Write" permissions. If the user is listed as "Everyone", the permissions may be overridden by a more specific group policy.
- Check for Antivirus interference. Some third-party antivirus software aggressively scans the Windows Temp folder. It may block VBA from saving files there, interpreting the action as a potential threat. Try temporarily disabling the antivirus to see if the script runs successfully.
- Run as Administrator. Right-click the Excel icon or the shortcut to the application you are using to run the macro. Select Run as administrator. This grants the application elevated privileges, which usually allows it to bypass standard user restrictions when accessing system folders.
- Check for Group Policy restrictions. In a corporate or school environment, IT administrators may enforce a policy that prevents users from writing to the Temp folder. If the above steps fail, the restriction is likely enforced at the operating system level and cannot be overridden by the user.
- Check the temp folder location. The system temp folder can sometimes be moved to a different drive or a network location that is currently unreachable or experiencing latency. You can check the current temp folder location by opening a command prompt and typing
echo %TEMP%. - Clear the VBA project lock. Occasionally, a corrupted VBA project lock file can prevent the editor from opening. Close all Office applications. Navigate to the %APPDATA%\Microsoft\VBA\VBA7.xlB folder and delete the lock file if it exists. Then reopen Excel and the VBA editor.
Frequently Asked Questions
Does this error happen on a Mac?
No, runtime error 735 is specific to the Windows operating system. The path syntax and permission structures in macOS differ significantly, and the error code will not appear on a Mac.
Why does it say "Can't save file to temp directory"?
The error message is a direct translation of the internal VBA error code 735. It means the SaveAs or FileCopy function was called with a path pointing to the system temporary directory, but the user account executing the script lacks the necessary write permissions to create or modify files in that directory.
Can I fix this by changing the registry?
No, modifying the registry is not a recommended solution for this specific issue. The problem is a file-system permission issue, not a registry configuration issue. Modifying the registry can cause system instability.
Is this a security risk?
Not necessarily. It is a symptom of how the application is trying to save a file. It is not a vulnerability in your computer. However, if you did not write the VBA code yourself and you receive this error, it is worth investigating why the code is trying to write to a temp directory.
Will this fix the error if I am using a network drive?
If the error occurs because the network drive is disconnected or the user lacks permissions on the network share, changing the temp path to a local drive will resolve the issue. However, if the script is designed to save to the network share for a specific reason, you must ensure the network connection and permissions are restored.
Why does the error happen even if I am an admin?
Even with administrator privileges, a standard user session may restrict access to certain system folders (like the Windows Temp folder) unless the application is explicitly run as an administrator. UAC policies often prevent even admins from writing to protected system areas unless the elevated prompt is active.