> ## 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.

# Finding the Temporary Folder with VBA
- URL: https://nolongerset.com/finding-the-temporary-folder-with-vba/
- Published: 2021-03-23T20:11:32.000Z
- Updated: 2026-05-08T13:05:43.000Z
- Description: Be a good steward of your users' file system. If you are creating temporary files, be sure to create them in the designated temporary folder.
- Author: Mike Wolfe
- Tags: Code Library, #FileFunctions, #Import 2026-05-20 02:59

If you need to create temporary files, you should create them in the Windows-designated temporary file folder. 

This is easy to find using the FileSystemObject's **[GetSpecialFolder](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/getspecialfolder-method)()** method. I could never remember the constant for the Temporary Folder–or the name of the method–so I wrote a simple function with an easy-to-remember name: TempPath().

## The Code: TempPath()

```vba
'---------------------------------------------------------------------
' Author    : Mike Wolfe
' Source    : https://nolongerset.com/finding-the-temporary-folder-with-vba/
' Purpose   : Returns something like:
'               C:\Users\Mike\AppData\Local\Temp\
' Notes     - Use PathJoin() function to simplify backslash handling
'               https://nolongerset.com/joining-paths-in-vba/
'---------------------------------------------------------------------
'
Function TempPath(Optional WithTrailingBackslash As Boolean = True) As String
    Static TempFolderPath As String

    If Len(TempFolderPath) = 0 Then
        Dim fs As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
        
        Const TemporaryFolder = 2
        TempFolderPath = fs.GetSpecialFolder(TemporaryFolder)
    End If
    TempPath = TempFolderPath
    If WithTrailingBackslash Then TempPath = TempPath & "\"
End Function
```

### Some notes about the code

Most built-in functions that return folders do so without including a trailing backslash. Before I created and started using my **[PathJoin()](https://nolongerset.com/joining-paths-in-vba/)** function, I found that I almost always wanted to have the trailing backslash when I was working with temporary folders. Thus, my TempPath function includes the trailing backslash by default.

Secondly, I cache the result of the GetSpecialFolder() call in a [static variable](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/static-statement). This makes no noticeable difference on a single call to the function. However, without this static variable, the constant creation and destruction of the FileSystemObject would likely impact performance if you called the function repeatedly within a loop.

*Image by [Free-Photos](https://pixabay.com/photos/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=846083) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=846083)*