Retrieving current user

D

DWTSG

Word XP
Windows 2000/XP mixed environment

ChangeFileOpenDirectory "C:\Documents and Settings\%username%\Desktop\"

is there a way to find the current user name that is logged into the
machine?
 
D

DWTSG

Thanks for the quick reply. If I make my variable 'user' and assign
applicaiton.username to it. How then, do I
put it in where %username%?
ChangeFileOpenDirectory "C:\Documents and Settings\%username%\Desktop\"
 
L

Lars-Eric Gisslén

Hi,

The easiest way to get any of the 'special folders' in Windows for the
current user is to use the following code.
(Include a reference to 'Microsoft Shell Controls and Automation' in your
VBA project)

Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder

Set oShell = New Shell32.Shell
Set oFolder = oShell.NameSpace(ShellSpecialFolderConstants.ssfDESKTOP)
' Program Files directory
' Set oFolder =
oShell.NameSpace(ShellSpecialFolderConstants.ssfPROGRAMFILES)
' Current users Templates directory
' Set oFolder =
oShell.NameSpace(ShellSpecialFolderConstants.ssfTEMPLATES)

' Current users Folder for the Start menu
' Set oFolder =
oShell.NameSpace(ShellSpecialFolderConstants.ssfSTARTMENU)
' and so on

MsgBox oFolder.Self.Path

Set oFolder = Nothing
Set oShell = Nothing

regards,
Lars-Eric
 
L

Lars-Eric Gisslén

Mn,

Unfortunately that will most often not work. Application.UserName will
return the name you use in Word and is usually not the same as your log on
name to the computer/network, which is required when getting the path to the
current users folders on the computer. Then you need the log on name to the
_computer_.

Regards,
Lars-Eric
 
D

Doug Robbins - Word MVP

Hi DWTSG,

See the article “How to get the username of the current user” at:

http://www.mvps.org/word/FAQs/MacrosVBA/GetCurUserName.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jan Kronsell

Wouldn't it be easier just to use

Environ("username")

or does this have a catch?

Jan
 

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