User name in File Path

S

Steve C

I am trying to write some code in Word where upon opening a certain template,
the user is prompted with an InputBox asking for his/her Windows login name,
such as "jsmith". I then want the user name to become part of a file path in
another line of code, as in the following example:

Dim MyName As String

MyName = InputBox ("Please enter your user name")

....Destination:= "C:\Documents and Settings\MyName\Application
Data\Microsoft\Templates\Normal.dot"

How do I write the file path so that MyName becomes part of the file path?
Thanks!

Steve C
 
D

Doug Robbins

Destination:= "C:\Documents and Settings\" & MyName & "\Application
Data\Microsoft\Templates\Normal.dot"

But, see the article "How to get the username of the current user" at:

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

and dispense with the need for the inputbox.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Steve Yandl

An alternate approach to extract the user's name which also avoids the input
box.


Set nwObj = CreateObject("WScript.Network")
strUser = nwObj.UserName


Steve
 
S

Steve Yandl

Not any simpler but this is a second approach that uses the
shell.application to return the path to the local user's Application Data
folder.

Set objShell = CreateObject("Shell.Application")
Set objLocAppFldr = objShell.Namespace(&H1C&)
strLocAppFldrPath = objLocAppFldr.Self.Path
strFullPath = strLocAppFldrPath & "\Microsoft\Templates\Normal.dot"


Steve
 
S

Steve C

Thanks Doug and Steve for all your help!

Steve Yandl said:
Not any simpler but this is a second approach that uses the
shell.application to return the path to the local user's Application Data
folder.

Set objShell = CreateObject("Shell.Application")
Set objLocAppFldr = objShell.Namespace(&H1C&)
strLocAppFldrPath = objLocAppFldr.Self.Path
strFullPath = strLocAppFldrPath & "\Microsoft\Templates\Normal.dot"


Steve
 

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