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

# GetTempPath: Using the Windows API for Maximum Performance
- URL: https://nolongerset.com/gettemppath/
- Published: 2022-08-09T03:20:08.000Z
- Updated: 2026-05-08T12:55:48.000Z
- Description: If you are looking to maximize performance in VBA, using the Windows API will almost always be your best bet.
- Author: Mike Wolfe
- Tags: Code Library, #FileFunctions, #Import 2026-05-20 02:59

I've already written an article about how to [use the FileSystemObject to retrieve the temporary folder path](https://nolongerset.com/finding-the-temporary-folder-with-vba/):

[Finding the Temporary Folder with VBABe a good steward of your users’ file system. If you are creating temporary files, be sure to create them in the designated temporary folder.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/map-846083_1920.jpg)](https://nolongerset.com/finding-the-temporary-folder-with-vba/)

Creating an instance of the FileSystemObject is a relatively expensive process–at least compared to calling a Windows API function. Let's look at a faster alternative.\*

*\* I should note that, unless it's being called inside a loop, creating an instance of the FileSystemObject is not something your actual end users are likely to notice. We're talking milliseconds here...*

## The Win API Function: GetTempPathA

We will be using the [GetTempPathA](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha) Windows API function.

The function returns the first path found from this list:

1. `TMP` environment variable
2. `TEMP` environment variable
3. `USERPROFILE` environment variable
4. The Windows directory

There are some [important caveats](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha#remarks):

> Note that the function does not verify that the path exists, nor does it test to see if the current process has any kind of access rights to the path. The **GetTempPath** function returns the properly formatted string that specifies the fully qualified path based on the environment variable search order as previously specified. The application should verify the existence of the path and adequate access rights to the path prior to any use for file I/O operations.

And one note that we can safely ignore:

> Apps should call [GetTempPath2](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a) instead of **GetTempPath**.

Why are we ignoring this? Because for our purposes GetTempPathA and GetTempPath2A will return identical results. Here's the relevant note from [GetTempPath2A's documentation](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a#remarks):

> For non-SYSTEM processes, **GetTempPath2** will behave the same as [GetTempPath](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha).

Hopefully this goes without saying, but you should not be running your Microsoft Access databases with SYSTEM-level permissions.

## The Code

Without further ado, here is the code:

```vba
'Place the following two lines in the module's header:
Private Declare PtrSafe Function apiGetTempPath Lib "kernel32" Alias "GetTempPathA" _
    (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

' ----------------------------------------------------------------
' Procedure : GetTempPath
' Date      : 8/8/2022
' Author    : Mike
' Source    : https://nolongerset.com/gettemppath/
' Purpose   : Uses the Windows API to return the path to the user's temp folder.
' Notes     : Returns a path including the trailing backslash.
' Usage     : Debug.Print GetTempPath
'             C:\Users\Mike\AppData\Local\Temp\
' ----------------------------------------------------------------
Function GetTempPath() As String
    'Maximum Windows path length is 260 characters
    Const MAX_PATH As Long = 260
    
    'Create a string and fill it with 260 spaces
    Dim Buffer As String
    Buffer = Space(MAX_PATH)

    'Call the API GetTempPathA function;
    '   this function returns the first path found from this list:
    '   1. TMP environment variable
    '   2. TEMP environment variable
    '   3. USERPROFILE environment variable
    '   4. Windows directory
    '
    '   It places the above value (with trailing slash) in the Buffer [out] argument
    '       and returns the length of the path which we use to trim the extra spaces.
    Dim PathLength As Long
    PathLength = apiGetTempPath(MAX_PATH, Buffer)
    
    'Trim the excess spaces from the buffer based on the returned path length
    GetTempPath = Left(Buffer, PathLength)
End Function
```

## Supporting Functions

I strongly recommend using my [PathJoin() function](https://nolongerset.com/joining-paths-in-vba/) as part of any solution that uses `GetTempPath()`:

[Joining Paths in VBAHow many times have you gotten a runtime error because you had duplicate or missing backslashes in your file paths? Never again!![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/02/spring-3404740_1920.jpg)](https://nolongerset.com/joining-paths-in-vba/)

If you are concerned that the folder may not exist, you can also use my [VerifyFolder() function](https://nolongerset.com/recursion-demystified-creating-subfolders/), which recursively creates any folders missing from a given path:

[Recursion Demystified: Creating SubfoldersRecursion: it’s not just for calculating factorials any more! A practical example of using recursion to create multiple missing subfolders.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2020/11/mirror-510976_1920.jpg)](https://nolongerset.com/recursion-demystified-creating-subfolders/)

---

### Referenced articles

[Finding the Temporary Folder with VBABe a good steward of your users’ file system. If you are creating temporary files, be sure to create them in the designated temporary folder.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/map-846083_1920.jpg)](https://nolongerset.com/finding-the-temporary-folder-with-vba/)

### External references

[GetTempPathA function (fileapi.h) - Win32 appsRetrieves the path of the directory designated for temporary files. (ANSI)Microsoft Docsalvinashcraft![](https://docs.microsoft.com/en-us/media/logos/logo-ms-social.png)](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha)

*Image by [Lee Rosario](https://pixabay.com/users/leerosario-4585232/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3922692) from [Pixabay](https://pixabay.com//?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3922692)*