Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Finding the Path to the Desktop.

Finding the Path to the Desktop

by Allen Wyatt
(last updated March 17, 2016)

3

Donald is writing a macro in which he needs to reference a user's desktop. However, the path to the desktop necessarily varies from system to system and user to user. He wonders what coding he can use to determine the path to the desktop regardless of system.

There are several ways to find the path to the desktop in VBA. One way is to call the Windows scripting host, in this manner:

Function GetDesktop() As String
    Dim oWSHShell As Object

    Set oWSHShell = CreateObject("WScript.Shell")
    GetDesktop = oWSHShell.SpecialFolders("Desktop")
    Set oWSHShell = Nothing
End Function

Note that this is a user-defined macro that you can use either from the worksheet or from another macro. The use from the worksheet would be as follows:

=GetDesktop()

Another way to determine the path to the desktop is to use the following line in your code:

sPath = Environ("USERPROFILE") & "\Desktop"

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8236) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Finding the Path to the Desktop.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Displaying Path Names in the Menu Bar

Want the full path name for a document visible on the screen? Easily add it to a menu bar.

Discover More

Saving Money on Printing Labels

Labels can be expensive, and a little common sense will help you waste less money as you try to get your labels to appear ...

Discover More

Calculating Averages by Date

When you have a huge amount of daily data to analyze, you may want to calculate an average of values for any given date in ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Disabled Macros

Do your macros seem to be disabled on your new machine? It could be because of the security settings in Excel. Here's where ...

Discover More

Pulling Apart Characters in a Long String

You can easily use formulas to pull apart text stored in a cell. For instance, if you need to pull individual characters from ...

Discover More

Adjusting a Path Based on System and User

It is not uncommon to set variables in a macro based on other values, such as time or date. You could also set variables ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 3?

2016-03-17 08:32:03

debriggs

I use the Environ method but test to make sure the path exists

Public Function PathExists(Pathname As String) As Boolean
If Len(Dir(Pathname, vbDirectory)) = 0 Then
PathExists = False
Else
PathExists = True
End If
End Function


2013-08-26 10:38:33

Jonathan Murray

Using the user profile directory and appending desktop will only work as long as Microsoft continues to use that structure, and only when your users haven't moved their 'desktop' folder (i.e. to a network asset to facilitate backups or the use of multiple workstations. The VBA solution seems more reliable.


2013-08-26 08:35:39

Bryan

What's the advantage/disadvantage of each method? Environ is easier to understand since it's a native VBA function, and it seems like it would be quicker since you don't have to dimension, create, and destroy an object (although I do it all on one line so I save a little time there), but I've seen the shell method so much on the internet that it really makes me wonder if it has some hidden advantage. Also, manually adding the "Desktop" makes me worry a bit, as I'm always paranoid that hard-coding a variable will fail in some unknown future.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.