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

Finding the Path to the Desktop

Written by Allen Wyatt (last updated July 11, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


5

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"

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8233) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Continuous Formatting for Footnotes

If you've got a lot of short footnotes in a document, you might be looking for a way to save space by "crunching up" the ...

Discover More

Replacing Hidden Text

Word allows you to format text so it can be easily hidden from view and from printing. If you want to convert the hidden ...

Discover More

Borders Disappear on Shaded Table Rows

When creating a table, it is possible that adding row or cell shading can affect how cell borders are displayed. This ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Determining the RGB Value of a Color

Excel allows you to fill a cell's background with just about any color you want. If you need to determine the RGB value ...

Discover More

Creating Add-Ins

Want to create your own add-in? Excel makes it easy to do. Here are all the steps you need.

Discover More

Displaying a Set Column Range

Do you want to display a particular range of columns within the Excel window? Here's a couple ways you can accomplish the ...

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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 5 + 5?

2023-05-11 05:17:42

Alan Elston

@ Michael
I will take a guess that special folders will always be referred to in English in VBA coding.

For example, I have German Excel, and I see my document folder written as
Dokumente
in the windows explorer window. But that will not work in the function from Allen Wyatt. However, if I use MyDocuments, then it does work and returns me
C:\Users\acer\Documents

That is an intersting result since if I use
Documents
in the function from Allen Wyatt, then that does not work either.
Strange. - In this case I have 3 different names for the same folder

Alan Elston

Incidentally, a quick google tells me that these are the so called "special folders", but they may apparantly vary a bit....
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates

When used for the Desktop, I have so far got consistant results in various language versions of Excel using
Desktop
in the function from Allen Wyatt.
I don't know about the other folders.


2023-05-10 16:14:12

Michael

Would a user with a different language require a reworked version of this? i.e. French as an example...would it require 'Bureau' instead of 'Desktop', or is the language for the lookup defaulted to use english?


2023-03-29 10:42:02

Arnaud

Thanks a lot, I needed that because the Desktop folder is moved on some of my customer workstations (OneDrive sync) and the other method I had based on username didn't work.


2023-02-17 06:53:36

Tom

i used Environ("USERPROFILE") & "\Desktop" all the time, but with the introduction of onedrive and the Desktop moved there that doesn't work anymore.

so your 'GetDesktop' is a nice function that always works!


2020-07-21 07:37:24

Mike D

I think I am missing something. The code makes sense and works only, sometimes.
When I copy the line =GetDesktop() from above and paste it into a sheet it works perfectly.
When I type it or paste matching 'Destination Formatting' it only pastes the text and does not execute the function.

What am I missing?

Thank you,
Mike D.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.