How can I retrieve the path to the Temp directory?
If you have a project that produces temporary files during runtime, it is good practice to make these files in the official Windows temporary directory. This directory is usually "c:\windows\temp", although it can vary with different flavours of windows. You can retrieve the path to the temporary directory by using the following code.
Declarations
You must put the following code in the declarations section of the project.
Declare Function GetTempPath Lib "kernel32" Alias _ "GetTempPathA" (ByVal nBufferLength As Long, ByVal _ lpBuffer As String) As Long Public Const MAX_PATH = 260
Code
Public Function GetTmpPath()
Dim strFolder As String
Dim lngResult As Long
strFolder = String(MAX_PATH, 0)
lngResult = GetTempPath(MAX_PATH, strFolder)
If lngResult <> 0 Then
GetTmpPath = Left(strFolder, InStr(strFolder, _
Chr(0)) - 1)
Else
GetTmpPath = ""
End If
End Function
Use
An example of how to use the function:
Call MsgBox("The temp path is " & GetTmpPath, vbInformation)