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

Changing Styles

Styles are a great boon to making your documents look better and making them easier to update. You can change the formatting ...

Discover More

Hanging Indents in Wrapped Text

If you use hanging indents for some of your paragraphs, you may wonder why they don't look right when they wrap on the right ...

Discover More

Getting Rid of the Lock Screen

The Lock Screen is handy on mobile devices, but may be a bother on your desktop PC. Here's how to turn the Lock Screen off so ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Positioning a Column on the Screen

If you have static columns and dynamic columns on the screen, you may want the dynamic columns to always show a particular ...

Discover More

Out of Memory Errors when Accessing the VBA Editor

It can be frustrating when you get error messages doing something that you previously did with no errors. If you get an out ...

Discover More

Determining an Integer Value

When creating macros, you often need to process numbers in various ways. VBA allows you to convert a numeric value to an ...

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. 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 five less than 5?

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.