Using Windows enviroment variables in Outlook macros

Last reviewed on June 17, 2013

If you need to use a Windows operating-system environment variable in a macro, you would use the Environ property. You can then use it in file paths, such as to save attachments or email to the user's My Documents folder.

Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))

For example, in the Save and Open Attachments macro, you could use the code snippet above to get the path to the user's folder instead of using shell32.

Instead of using this to get the file path:

strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)

You could use this:

Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))
strFolderpath = enviro & "\OLAttachments\"

Or this:

Set oShell = CreateObject("WScript.Shell")
strFolderpath = oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Documents\"

For a complete list of the Environs, run this code and review the list in the Immediate windows (Ctrl+G)

  Sub EnumSEVars()
        Dim strVar As String
        Dim i As Long
        For i = 1 To 255
            strVar = Environ$(i)
            If LenB(strVar) = 0& Then Exit For
            Debug.Print strVar
        Next
    End Sub

Below is a list of some of the available environment variables, which are likely to be useful in Outlook programming. The can also by typed or pasted into the Address bar of Windows Explorer to jump to the folder location.

Shell FolderPath
AppData (Windows XP, 7/8)%USERPROFILE%\Application Data
AppData (Windows 7/8 only)%APPDATA%
Local AppData (Windows XP, 7/8)%USERPROFILE%\Application Data
Local AppData (Windows 7/8 only)%LOCALAPPDATA%
Path to user's profile (e.g. C:\Users\{username})%HOMEPATH%
Desktop%USERPROFILE%\Desktop
My Music (WinXP)%USERPROFILE%\My Documents\My Music
My Pictures%USERPROFILE%\My Documents\My Pictures
Personal [My Documents] (WinXP)%USERPROFILE%\My Documents
Personal [My Documents] (Windows 7/8)%USERPROFILE%\Documents
Templates (WinXP)%USERPROFILE%\Templates

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999 and involved in IT support since 1985, Diane is the author of several books and video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.