• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Using Windows environment variables in Outlook macros

Slipstick Systems

› Developer › Using Windows environment variables in Outlook macros

Last reviewed on October 23, 2014     10 Comments

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
Using Windows environment variables in Outlook macros was last modified: October 23rd, 2014 by Diane Poremsky
Post Views: 52

Related Posts:

  • Browseforfolder_2
    How to use Windows File Paths in a Macro
  • Save Messages and Attachments to a New Folder
  • Saving All Messages to the Hard Drive Using VBA
  • Save all incoming messages to the hard drive

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created 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.

Comments

  1. Mike Durthaler says

    September 7, 2017 at 10:16 am

    Glad to find this article. Here's what I'd like to do -- organize what emails go to Outlook 2016 (Office 365 version) on 2 different machines. I have a machine for personal and educational stuff for which my company gives all of us a full MSDN license so we can keep our skills up. But -- no for profit work can be done with this.

    So, my for profit work is done on another machine. I'd like to set up Outlook on both such that when I see a certain From address I can set up a rule for it from either machine. If it's educational, from either machine, put in that environment variable. If it's for profit, same deal from either machine.

    Someone may have already thought of this. If not, if you've got some links that would save me search time, could you fire them my way? If there's a paid solution out there, say someone will sell their code for 100 bucks or so it's worth it to me to pay them for it.

    Thanks,

    Mike Durthaler

    Reply
    • Diane Poremsky says

      September 8, 2017 at 11:31 pm

      This is with an exchange mailbox? So you want to filter specific addresses to the profit folder, other addresses to the education folder? You could do this with Outlook's rules. Assuming exchange mailbox, they should be server-side rules and will work on all computers (they run as mail arrives, not when outlook downloads the mail).

      If its more complicated than that, you could use either a run a script rule or an itemadd macro.

      Reply
  2. Murugan says

    December 29, 2016 at 3:31 am

    Hi Diane, Hope you are doing good. Coming back to you after 2 years of successful implementation of codetwo Properties :-) coupled with an excel macro to retrieve every 5 min of my teams responses. This time, i am inneed of putting environment variable inside shared email box so that i know who has responded. Is there a way to implement this particular thing in your code two proerties?

    Thanks
    Murugan

    Reply
    • Diane Poremsky says

      December 29, 2016 at 4:24 pm

      run the EnumSEVars macro and look at the immediate field for the list of environment variables available and the results. %username% will give you the logged in computer user, which is usually the user's folder name: c:\users\this-name. If users don't share workstation logons, this will be the easiest/shortest code. You could get the logged in mailbox name - i should have a code sample for that here somewhere.

      Reply
      • Murugan says

        January 3, 2017 at 4:46 am

        Hi Diane,
        Thanks for your reply and indeed I checked with EnumSEVars macro and tried to get the environment variable into a column in outlook. However i am getting my name as against the users who had actually sent the emails.

        Please help.

        Thanks
        Murugan

      • Diane Poremsky says

        January 3, 2017 at 9:34 am

        if you want the sender's name you need to use item.sendername (where item is replaced by whatever you are using to refer to the message object) - that will get the display name the sender uses.

      • Murugan says

        January 10, 2017 at 10:05 am

        Hi Diane, Sorry i tried all these days only to fail :-(. I will explain you in detail.
        1) have a Customer support team of 45 members working in Chennai, Banglore and Hyderabad using shared email account sharedemail@company.com
        2) With your Extended Properties cfg file, i was able to figure out and track the turn around time of email responses inside outook.
        3) have developed excel macro (with your support 2 years back) which runs every 5 minutes (with application.ontime method) and presents to the management (connected to big LCD screen) about the status of 3 locations - Pending, Time taken to respond, Replied or Forward.
        4) However, recently there was an escalation about incorrect email response been made by some of our team member and dont know who had responded. I had to take a blame for that. So, to track that, i need your help in capturing the system name or user name by way of "Replied by User".
        Hoping for solution :-)

      • Murugan says

        January 11, 2017 at 7:52 am

        Hi Diane,
        Not sure whether my previous comment reached you but sorry i tried item.sendername but i am not getting. Infact i am actually looking like this..PR_Replier_Email_Address. This will easily fix my job whenever team responds incorrect email

      • Diane Poremsky says

        January 12, 2017 at 2:18 pm

        The earlier comment is here, but i am way behind in answering. :(

      • Murugan says

        January 19, 2017 at 8:13 am

        Hi Diane
        Sorry to bug you over this again and again. Any update on this :-).

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 31 Issue 7

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Use Classic Outlook, not New Outlook
  • How to Remove the Primary Account from Outlook
  • Reset the New Outlook Profile
  • Disable "Always ask before opening" Dialog
  • This operation has been cancelled due to restrictions
  • How to Hide or Delete Outlook's Default Folders
  • Change Outlook's Programmatic Access Options
  • Use Public Folders In new Outlook
  • Removing Suggested Accounts in New Outlook
  • Shared Mailboxes and the Default 'Send From' Account
  • Sync Issues and Errors with Gmail and Yahoo accounts
  • Error Opening iCloud Appointments in Classic Outlook
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Sync Issues and Errors with Gmail and Yahoo accounts

Error Opening iCloud Appointments in Classic Outlook

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2026 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.