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

How to use Windows File Paths in a Macro

Slipstick Systems

› Developer › Code Samples › How to use Windows File Paths in a Macro

Last reviewed on March 3, 2022     35 Comments

When you use a VBA macro to save messages or files you can hard code the file path, use the user account's My Documents folder, or select a folder on the hard drive. The first two methods are fairly short and sweet.

Note: this function will work in any Office application, it is not specific to Outlook. (Actually, it's not specific to Office either, it's a general VB function.)

Dim strFolderpath  as String
strFolderpath = "C:\OLAttachments\"

Or use this to save to a folder using the user's profile in the path (example, C:\Users\Diane\)

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

For more examples, see Using Windows environment variables in Outlook macros

If you want to bring up a folder picker dialog, you need to use a function, such as the one below.

Add the function to your project and call the folderpath using the following format. You can use any valid file path, either a drive letter or network path (\\). If the path is not valid, the folder picker will show all folders (desktop level), as seen in the screenshot.
Browse for a folder to save files to

strFolderpath = BrowseForFolder("C:\Users\username\documents\")

Use this to save the file:
strFile = strFolderpath & "\" & strFile

(The lines above can be used in Save Attachments to the hard drive)

BrowseForFolder Function

Function BrowseForFolder(Optional OpenAt As Variant) As Variant
  Dim ShellApp As Object
  Set ShellApp = CreateObject("Shell.Application"). _
 BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
 
 On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
 On Error GoTo 0
 
 Set ShellApp = Nothing
    Select Case Mid(BrowseForFolder, 2, 1)
        Case Is = ":"
            If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
        Case Is = "\"
            If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
        Case Else
            GoTo Invalid
    End Select
 Exit Function
 
Invalid:
 BrowseForFolder = False
End Function

Save attachments macro using BrowseForFolder

This macro uses BrowseForFolder function when saving email attachments on the selected message. When you use this function, you can browse to or create subfolders but cannot "browse up" to a folder above the preselected root.

browse for folder to save attachments

Public Sub SaveAttachmentstoFolder()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim i As Long
Dim lngCount As Long
Dim StrFile As String
Dim strPath As String
Dim StrFolderPath As String
Dim strDeletedFiles As String
Dim sFileType As String
    
On Error Resume Next

Set objOL = CreateObject("Outlook.Application")
Set objMsg = objOL.ActiveExplorer.Selection.Item(1)
 
' Get the BrowseForFolder function http://slipstick.me/u1a2d   
StrFolderPath = BrowseForFolder("D:\My Stuff\Email Attachments\")
 
    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
         
    If lngCount > 0 Then
     
    For i = lngCount To 1 Step -1
     
    StrFile = objAttachments.Item(i).FileName
    StrFile = StrFolderPath & "\" & StrFile
    objAttachments.Item(i).SaveAsFile StrFile
    
    Next i
    End If
          
ExitSub:
 
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub

To make this macro portable when you use a folder under the user's directory, replace StrFolderPath = BrowseForFolder("D:\My Stuff\Email Attachments\") with the following:

Dim StrUserPath as Variant
Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))

StrUserPath = enviro & "\Documents\Email Attachments\"
StrFolderPath = BrowseForFolder(StrUserPath)

How to use Windows File Paths in a Macro was last modified: March 3rd, 2022 by Diane Poremsky

Related Posts:

  • Save Messages and Attachments to a New Folder
  • Use VBA to open Outlook messages stored in the file system
  • Save and Open an Attachment using VBA
  • Saving All Messages to the Hard Drive Using VBA

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.

Subscribe
Notify of
35 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Pedro
March 13, 2023 10:37 am

Hi Diane,
i'd like to modify the macro to prompt for a folder location on a file as dialog.
Is it possible?

0
0
Reply
Diane Poremsky
Author
Reply to  Pedro
March 13, 2023 11:02 pm

If you want to use the office file pocker from word or excel, I have an example here - https://www.slipstick.com/outlook/hyperlink-templates/#fileopen

it uses objectname.FileDialog(msoFileDialogFolderPicker) - Outlook doesn't have a filedialog of its own, so you need to use word or excel's.

0
0
Reply
Yohann
July 22, 2020 11:40 am

Hello, thank you for the code BrowseForFolder. Do you know if it possible to open a more complete browser (I mean with favorites and also shortcuts)?Thank you.
Yohann

0
0
Reply
Diane Poremsky
Author
Reply to  Yohann
July 22, 2020 5:22 pm

You can use the file open browser (or Save) from word or excel to see all folders.

https://www.slipstick.com/outlook/hyperlink-templates/#fileopen

2020-07-22_17-22-54-0000.png
1
0
Reply
Yohann
Reply to  Diane Poremsky
July 28, 2020 3:54 am

Thank you.

0
0
Reply
Zsolt
September 4, 2018 3:26 am

I seem to have some problem with the function. For some reason it takes the environment variable, opens the select folder window. I select it, then it exits the function and immediately calls the function again. This time, the environment variable is not passed. I tried to figure out why the function is called twice, but not sure. Maybe any ideas?

0
0
Reply
Diane Poremsky
Author
Reply to  Zsolt
September 5, 2018 1:10 am

Are you using the macro code on this page or other code?

0
0
Reply
Reuben Dayal
July 26, 2018 7:44 am

Hi Diane,

This code is working great! I want to ask you if the folder picker can let me select shortcut links as well? I have all my important folder shortcuts saved in \Documents\Favourites folder. However, the current folder picker only allows me to select a folder and not further quick navigate to the final folder via my shortcuts. Any suggestions?

Thank you so much.

0
0
Reply
Diane Poremsky
Author
Reply to  Reuben Dayal
September 5, 2018 1:08 am

No, it won't let you select shortlinks. Sorry. You'd need to write a custom picker. It wouldn't be hard, if you only ever wanted to use those links... as you wont be able to select other folders, only the links.

0
0
Reply
Steve Underhill
April 23, 2018 12:50 pm

Hi Diane - great work you do here - thanks!

I have BrowseForFolder working perfectly. is there a way to hard code only a handfull of choices, like 4, subfolders from the folder that BrowseForFolder returns? I ask because on my corporate network there are dozens of subfolders in the returned folder but I only use 3 or 4 of them at anytime. The drive is shared by many so I can't move my folder into their own subfolder.

Thanks again!

0
0
Reply
Diane Poremsky
Author
Reply to  Steve Underhill
April 23, 2018 9:50 pm

AFAIK, no, you can only hard code the parent folder.

0
0
Reply
Steve
Reply to  Diane Poremsky
April 24, 2018 9:44 am

Ok, thanks.

0
0
Reply
Antonio
September 15, 2017 8:57 pm

Hi Diane Poremsky

On Outlook 2013 I'm looking for a specific macro that could change the default path for the Attach File Command Button at Ribbon every time a call a macro .
Can you please post some code that can help me
Thanks!

0
0
Reply
nathan
March 1, 2017 8:26 am

Your website has help massively with what I'm trying to achieve, I have got my emails saving to my documents but I'm wanting to add to the macro so you can select the folder on a server using a input text box. how would this be done. I'm not clued up on VBA code so any assistance would be greatly appreciated.

0
0
Reply
Diane Poremsky
Author
Reply to  nathan
May 26, 2017 1:33 am

You'd use the network path - \\server\path in the link.

0
0
Reply
Kaustubh Thakur
December 22, 2016 1:08 pm

Ican't get the Browsefor Folder function to work. The savemsg macro is working as expected and I can make a copy of the desired msg in the "My Documents" folder. How do I incorporate the browseforfolder function? Thanks in advance!

0
0
Reply

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

Latest EMO: Vol. 30 Issue 31

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
  • Jetpack plugin with Stats module needs to be enabled.
  • 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
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
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

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

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

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 © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

:wpds_smile::wpds_grin::wpds_wink::wpds_mrgreen::wpds_neutral::wpds_twisted::wpds_arrow::wpds_shock::wpds_unamused::wpds_cool::wpds_evil::wpds_oops::wpds_razz::wpds_roll::wpds_cry::wpds_eek::wpds_lol::wpds_mad::wpds_sad::wpds_exclamation::wpds_question::wpds_idea::wpds_hmm::wpds_beg::wpds_whew::wpds_chuckle::wpds_silly::wpds_envy::wpds_shutmouth:
wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment