Search notes:

Examples for Visual Basic for Application accessing the Windows API: GetTempFileName

The Windows API function GetTempFileName returns the name for a temporary file. It is often used in conjuction with GetTempPath.
The following example needs the VBA declarations of the Windows API which can be found here.
sub main()

   dim tempPath as string * 512
   dim tempFile as string * 512 ' Should be MAX_PATH !
   dim uUnique  as long
   

   call GetTempPath(512, tempPath)

   uUnique = 0 ' Create unique temporary file names
   call GetTempFileName(tempPath, "abc", uUnique, tempFile)

   msgBox "The temporary file is " & tempFile

 '
 ' The temporary file is not yet created. Open it and
 ' write something into it:
 '
   open tempFile for output as 1

   print# 1, "Foo bar baz"
   print# 1, "One two three"

   close# 1

end sub
Github repository WinAPI-4-VBA, path: /examples/GetTempFileName.bas

See also

The VBA open statement.
Other examples

Index