> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Getting the Temp Folder in VBA
- URL: https://nolongerset.com/getting-the-temp-folder-in-vba/
- Published: 2022-10-01T03:22:38.000Z
- Updated: 2026-05-08T12:54:28.000Z
- Description: There are many ways to get the temporary folder in VBA. But if you look behind the curtain, there's really only one...
- Author: Mike Wolfe
- Tags: Code Library, #FileFunctions, #Import 2026-05-20 02:59

**tl;dr**: Just use `Environ("TMP")`.

---

There are several ways you can get the temporary folder in VBA:

- `Environ("TMP")`: use the [Environ() VBA function](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/environ-function) to return the value of the user's `TMP` environment variable
- `Environ("TEMP")`: return the value of the user's `TEMP` environment variable (don't ask me, [ask Raymond Chen](https://devblogs.microsoft.com/oldnewthing/20150417-00/?p=44213))
- `FSO.GetSpecialFolder(2)`: use the File System Object's [**GetSpecialFolder** function](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/getspecialfolder-method)
- `TempPath()`: my custom [**TempPath()** function](https://nolongerset.com/finding-the-temporary-folder-with-vba/) (which uses `FSO.GetSpecialFolder()`)
- `GetTempPath()`: my custom [**GetTempPath()** function](https://nolongerset.com/gettemppath/) (which uses API function [GetTempPathA](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha))

### GetTempPath() API

Internally, the GetTempPath API function searches for temporary folder paths using the following [priority](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha#remarks):

1. The path specified by the TMP environment variable.
2. The path specified by the TEMP environment variable.
3. The path specified by the USERPROFILE environment variable.
4. The Windows directory.

### FSO.GetSpecialFolder(2)

The *folderspec* argument of GetSpecialFolder can have [one of three values](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/getspecialfolder-method#settings). 

Value `2` represents the **TemporaryFolder**:

> The Temp folder is used to store temporary files. Its path is found in the TMP environment variable.

## My Advice

This is one of those topics where I've changed my opinion after learning new information. 

For a long time, I resisted using the `Environ()` function because it seemed *too easy*. And so I created not one–but two (!)–custom functions to do what `Environ()` does for free.

So, now, I'll repeat my advice from above:

**Just use `Environ("TMP")`.**

- That's what `FSO.GetSpecialFolder(2)` does.
- That's the first search priority of the `GetTempPathA()` Windows API function.
- It's very easy to do in VBA.

You gain a tiny bit of additional reliability with the API call in the exceedingly rare instance that the user's `TMP` environment variable has been deleted. I no longer think that tradeoff makes sense.

## One Big Caveat – The Trailing Backslash

There is no consistency among the many ways to retrieve a temp folder of whether the value returned includes a trailing backslash. I suggest you use some simple code to add–or remove–the trailing backslash as needed.

In the code below, I use `foTemp` as the variable name that holds the temporary folder path.

### Code to Ensure a Trailing Backslash

```vba
If Right(foTemp, 1) <> "\" Then foTemp = foTemp & "\"
```

### Code to Ensure NO Trailing Backslash

```vba
If Right(foTemp, 1) = "\" Then foTemp = Left(foTemp, Len(foTemp)-1)
```

### PathJoin()

Alternatively, you can just use [my PathJoin() function](https://nolongerset.com/joining-paths-in-vba/) and not have to worry about backslashes at all.