Generic name for User folder?

W

wal

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.
 
K

Klaus Linke

wal said:
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
 
J

Jonathan West

wal said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top