Consulting

Results 1 to 4 of 4

Thread: Returning the Desktop Path

  1. #1
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    1
    Location

    Returning the Desktop Path

    How can I return the desktop path?

    I have a macro that imports and parses a desktop text file in Excel. The macro is tied to a button within a macro workbook which will be distributed to other users that have different desktop addresses than mine. Hence, they either would have to change the VBA script manually - not desirable, or possibly I can run some script that will return the desktop path?

    Please advise.


    Thanks!


    Brian

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    There's a bunch of Namespace options, but this gets the current user's desktop


    Option Explicit
    
    Sub test()
        MsgBox Desktop_Path
    End Sub
    
    Function Desktop_Path() As String
        Const sf_DESKTOP As Variant = 0
        Desktop_Path = CreateObject("Shell.Application").Namespace(sf_DESKTOP).Self.Path
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    or

    msgbox CreateObject("wscript.shell").specialfolders(4)

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    To be cross-platform
    Function Desktop_Path() As String
        Const sf_DESKTOP As Variant = 0
        #If Mac Then
            Desktop_Path = Mid(MacScript("tell application ""Finder""" & vbLf & "return desktop as alias" & vbLf & "end tell"), 7)
        #Else
            Desktop_Path = CreateObject("Shell.Application").Namespace(sf_DESKTOP).Self.Path
        #End If
    End Function

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •