yashika vaish
yashika vaish

Reputation: 201

Vba code to open file from a specific path that works for every user except temp

Workbooks.Open Filename:="C:\Desktop\VBA\Phase 1\A.xlsx"

How do I open the file from desktop or documents of any user.

Upvotes: 1

Views: 4925

Answers (1)

0m3r
0m3r

Reputation: 12499

Multiple ways you can do that, pick the one that works for you-

Option Explicit
Public Sub Example()
    Dim FilePath As String
    FilePath = CreateObject("WScript.Shell") _
                  .specialfolders("Desktop") & "\VBA\Phase 1\A.xlsx"
    Debug.Print FilePath
End Sub

Option Explicit
Public Sub Example2()
    Dim FilePath As String
    FilePath = Environ("USERPROFILE") & "\Desktop\VBA\Phase 1\A.xlsx"
    Debug.Print FilePath
End Sub

Upvotes: 3

Related Questions