Link to home
Start Free TrialLog in
Avatar of robjohnston
robjohnston

asked on

My Documents Path

Hi folks.

I'm writting a little backup program which will get certian files from the "My Document" folder.

The program will be for Win98 and Win2K.

Is the an easy way of finding the path to "My Documents". Is it anything like app.path?

Thanks, Rob.
Avatar of rspahitz
rspahitz
Flag of United States of America image

For Win2000, you can use this:

MyDocPath = Environ$("UserProfile" & "\My Documents")

For Win98, I think you can use this:

MyDocPath = "C:\My Documents"

ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bob_online
bob_online

You know, the user can rename "My Documents" to anything they want...

This method will find "My Documents" no matter what it is called:

Public Property Get MyDocuments() As String

   On Error Resume Next
   
   Set mWindowsScriptingObject = CreateObject("wscript.shell")
   MyDocuments = mWindowsScriptingObject.RegRead("HKEY_CURRENT_USER\" & _
      "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

   Set mWindowsScriptingObject = Nothing

End Property


This may not work on win 2000  (I haven't tried it).
Sorry, left this part out:

   Private mWindowsScriptingObject     As Object
Looks good on Win2K, bob_online:

Private Sub Command1_Click()

  On Error Resume Next
  Dim mWindowsScriptingObject As Object
 
  Set mWindowsScriptingObject = CreateObject("wscript.shell")
  MsgBox mWindowsScriptingObject.RegRead("HKEY_CURRENT_USER\" & _
     "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

  Set mWindowsScriptingObject = Nothing

End Sub
Now you have to worry about the user having the latest scripting engine installed (for use of the wscript.shell class)...I would stick to the API method I linked to above.  If it seems too complicated, here is the stripped down version:



Option Explicit

Private Declare Function SHGetFolderPath Lib "shfolder" Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwReserved As Long, ByVal lpszPath As String) As Long
Private Const CSIDL_PERSONAL As Long = &H5


Private Sub Command1_Click()
    Dim strPath As String
 
    'fill pidl with the specified folder item
    strPath = Space$(260)
   
   If SHGetFolderPath(Me.hWnd, CSIDL_PERSONAL, 0&, &H0, strPath) = 0 Then
       MsgBox Left$(strPath, InStr(strPath, Chr$(0)) - 1)
   End If
End Sub
Avatar of robjohnston

ASKER

Thanks to everyone.

I have chosen AzraSound's solution as it fits the bill the best and gives me an easy way of backing up over user info. The program will only run on Win98SE and Win2k so there aren't any worries there.

Cheers, Rob.