My Documents Default path in VBA - Irrespective of Current User

raghuram.star

Board Regular
Joined
Sep 5, 2012
Messages
102
I want to assign "My Documents" Path to a Variable, which should assign the Path in which ever system I run this Macro

Eg: Path of My Documents in my PC
C:\Documents and Settings\Rams\My Documents\

But If I hard code it in VBA, it will not work in other PCs with different User Names

Is there a way to assign default path of My Documents to an variable? So that I can run my macro from any where...!!

Any suggestions will be great help

Thanks in advance
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Paste this code into a new module.
Code:
Option Explicit
     ' Declare for call to mpr.dll.
   Declare Function WNetGetUser Lib "mpr.dll" _
      Alias "WNetGetUserA" (ByVal lpName As String, _
      ByVal lpUserName As String, lpnLength As Long) As Long
   Const NoError = 0       'The Function call was successful

   Function GetUserName()
      ' Buffer size for the return string.
      Const lpnLength As Integer = 255
      ' Get return buffer space.
      Dim status As Integer
      ' For getting user information.
      Dim lpName, lpUserName As String
      ' Assign the buffer size constant to lpUserName.
      lpUserName = Space$(lpnLength + 1)
      ' Get the log-on name of the person using product.
      status = WNetGetUser(lpName, lpUserName, lpnLength)
      ' See whether error occurred.
      If status = NoError Then
         ' This line removes the null character. Strings in C are null-
         ' terminated. Strings in Visual Basic are not null-terminated.
         ' The null character must be removed from the C strings to be used
         ' cleanly in Visual Basic.
         lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
      Else
         ' An error occurred.
         MsgBox "Unable to get the name."
         End
      End If
      GetUserName = lpUserName
   End Function

Function MyDocs() As String
    Dim strStart As String
    Dim strEnd As String
    Dim strUser As String
    
    strUser = GetUserName()
    strStart = "C:\Documents and Settings\"
    strEnd = "\My Documents\"
    
    MyDocs = strStart & strUser & strEnd
End Function

To get the user's documents path do something like:

Code:
Dim strDocs As String

strDocs = MyDocs()

Denis
 
Upvote 0
Code:
Function mydocs () as string
Set WshShell = CreateObject("WScript.Shell")
Mydocs= WshShell.SpecialFolders("MyDocuments")
End function
 
Upvote 0
Jim, that's much neater than my suggestion.
Thanks -- I didn't think of using Windows Scripting.

Denis
 
Upvote 0
Cheers, I also use something similar for desktop location, I know some people redirect MyDocuments, so let windows look after it, hence the short code
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top