Generic name for User folder?

  • Follow


Word 2007
Vista Enterprise

I have a macro that tests for a certain path; the path is something
like:

C:\Users\JohnSmith\Documents

Other people in my organization might find this macro useful.  Is
there a generic way to designate the person's User folder (i.e., the
"JohnSmith" folder in my example) in VBA, so I don't have to rewrite
the path code for each person?

Thanks.
0
Reply wal 12/16/2009 5:03:47 PM

"wal" <ress6oy8@verizon.net> wrote:
> Word 2007
> Vista Enterprise
>
> I have a macro that tests for a certain path; the path is something
> like:
>
> C:\Users\JohnSmith\Documents
>
> Other people in my organization might find this macro useful.  Is
> there a generic way to designate the person's User folder (i.e., the
> "JohnSmith" folder in my example) in VBA, so I don't have to rewrite
> the path code for each person?


You should be able to retrieve it from the Registry...
Not sure, I guess it would be
System.PrivateProfileString("", 
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell 
Folders", "Personal")

You can also get Word's default file path with
Options.DefaultFilePath(Path:=wdDocumentsPath)
.... but that may be set to whatever path the user last used, and likely is 
not what you want.

Regards,
Klaus


-1
Reply Klaus 12/16/2009 5:34:00 PM


"wal" <ress6oy8@verizon.net> wrote in message 
news:7148f4a7-9164-4818-83bf-ebfd91dc0f4b@g12g2000vbl.googlegroups.com...
> Word 2007
> Vista Enterprise
>
> I have a macro that tests for a certain path; the path is something
> like:
>
> C:\Users\JohnSmith\Documents
>
> Other people in my organization might find this macro useful.  Is
> there a generic way to designate the person's User folder (i.e., the
> "JohnSmith" folder in my example) in VBA, so I don't have to rewrite
> the path code for each person?
>

Karl Peterson has written a class module you can drop into your project, 
which will retrieve for you a whole range of special folders.
http://vb.mvps.org/samples/SysFolders/

Much better to use the Windows API calls for this (as Karl does) than to 
attempt to guess the folder paths or even retrieve them directly from the 
registry. Folder locations and even registry keys come & go, but even 
Microsoft would not dare to mess with the API calls - to do so would cause 
just about every significant Windows program in existence to stop working.

So using Karl's class module means that as folder locations change between 
versions of windows, your code will still work. For instance, under Windows 
XP, the equivalent folder is C:\Documents and Settings \JohnSmith\My 
Documents. The code will provide the appropriate folder depending on the 
user name and the version of Windows.


-- 
Regards
Jonathan West 

0
Reply Jonathan 12/16/2009 6:21:27 PM

2 Replies
913 Views

(page loaded in 0.06 seconds)

6/5/2014 10:54:42 AM


Reply: