Solved

Find location of “My Documents” with VBA in Windows 7

Posted on 2010-09-13
10
2,879 Views
Last Modified: 2012-05-10
Because my drive C: was getting too filled, I moved "My Documents" from its usual location of

                “C:\Documents and Settings\" & Environ("USERNAME") & "\" & “My Documents”

to the same location on drive “F:”

     I would like to have VBA be able to recognize where the new location of “My Documents” is. Other persons who use my programs may also have moved the location of “My Documents,” so any suggestions as to how VBA can find that folder would be most welcome.

     Note: my Windows Explorer indicates that drive C: still has a directory named “Documents and Settings” but if one clicks on it, a message appears that the directory is not accessible.

     I suppose one could check each drive for the existence of a “My Documents,” but I do not know how to make a loop that could look at each drive on a computer.

          Thanks, as always, for your help
          John Robin
0
Comment
Question by:JohnRobinAllen
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:Fixforyou
ID: 33662888
Did you use the properties page to move the my documents folder?  The actual path is now {DRIVELETTER}:\Users\{username}\   if I remember correctly.  Your script will have to point to that location I would think, but then it would be pointed to a symbolic link.
movedocuments3.png
0
 
LVL 3

Expert Comment

by:danlein
ID: 33662900
Can't you just put it in a library ?
0
 
LVL 3

Expert Comment

by:Fixforyou
ID: 33662915
Oppps, forgot to add the string variable
%HOMEPATH%\My Documents

see what that does
0
 
LVL 3

Expert Comment

by:d_willia
ID: 33662960
Here is the reg entry for the setting:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal
 
0
 
LVL 3

Expert Comment

by:Fixforyou
ID: 33663025
if "%HOMEPATH%\My Documents" does not work make sure you have ownership of the drive or files...

For taking ownership of files, rightclick them, Security
tab, Advanced, Owner tab. In Name list select Administrator or Admin-group and click OK. Click Add and in the "Enter the object...list" type a user or group account or Administrator. Click OK. In the group or user name list, click the account you want (e.g. Administrator), then tick the check boxes for permissions, e.g. Full Control [Allow].
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Expert Comment

by:FEOX
ID: 33675065
Assuming you did move the My Documents folder using Fixforyou's method (or through registry or through Environment variables), here is a VBA code that retrieves the "My Documents" folder of the current user:
'Set MyDocsPath to Current User's My Documents Folder:

Dim WshShell As Object

Dim MyDocsPath

Set WshShell = CreateObject("Wscript.Shell")

Set MyDocsPath = WshShell.SpecialFolders("MyDocuments")

Set WshShell = Nothing



'Display MyDocsPath:

MsgBox "My Documents folder: " & MyDocsPath



'More info here:

'http://msdn.microsoft.com/en-us/library/0ea7b5xe(VS.85).aspx

Open in new window

0
 

Author Comment

by:JohnRobinAllen
ID: 33677054
I am very excited about all the suggested solutions and will get back to EE probably tomorrow to describe the results. Many thanks for all this help. I wish that other commitments were not preventing me from responding today.
       john robin
0
 

Author Comment

by:JohnRobinAllen
ID: 33686322
I tried FEOX's code and it crashes on line 5:
     Set MyDocsPath = WshShell.SpecialFolders("MyDocuments")
A message appears "Run time error '13'"   Type mismatch.

I do not understand how to translate the other comments into VBA code to run on another user's machine.

     I hope the problem is solvable.

    j.r.a.




0
 
LVL 2

Accepted Solution

by:
FEOX earned 250 total points
ID: 33687259
Oops, try removing the "Set " on that line:

Use this, instead:
'Set MyDocsPath to Current User's My Documents Folder:

Dim WshShell As Object

Set WshShell = CreateObject("Wscript.Shell")

MyDocsPath = WshShell.SpecialFolders("MyDocuments")  'Don't use Set

Set WshShell = Nothing



'Display MyDocsPath:

MsgBox "My Documents folder: " & MyDocsPath



'More info here:

'http://msdn.microsoft.com/en-us/library/0ea7b5xe(VS.85).aspx

Open in new window

0
 

Author Closing Comment

by:JohnRobinAllen
ID: 33704748
The solution works like a charm, but needs one small change for those of us who like to use "Option Explicit."

I added the line
     Dim MyDocsPath As String
and the code works perfectly.

Many, many thanks for such an easy-to-implement solution
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After playing around with my ASUS 1215n (http://www.asus.de/product.aspx?P_ID=HrglRhH8D60Rmlv3) Netbook, I finally managed to get smooth HD 1080p (http://en.wikipedia.org/wiki/1080p) playback of videos on it. Second Generation Intel Atom (http://en.…
The Display applet of Windows 7 Control Panel has changed a great deal since Windows XP  (it was missing and more or less replaced in Windows Vista by the Personalization applet.)  Below is a screenshot of what the Display applet of Windows XP, whic…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now