Username in macro

L

Laura G

Is there an easy way to prompt the user for their username
using a User Form and textbox and then pull that
field "txtUser" into a macro? I am trying to pull the
username into code that references this directory on each
user's PC: c:\Documents and Settings\USERNAME\Application
Data\.
 
J

Jim Faraone

Insert a new module or a new class....and paste the following code into it.

Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long

Function GetUserNameA() As String
Dim lpBuff As String * 25
Get_User_Name lpBuff, 25
GetUserNameA = LTrim(RTrim(Left(lpBuff, Len(lpBuff) - 1)))
End Function

this function will get the user's network id without prompting them.
 
L

Laura G

Hi Jim,

Thanks for the code for the function to get the user's
network id. That seems better than prompting the user for
it. After the function runs, how do I transfer the
username into the macro for the path: c:\Documents and
Settings\USERNAME\Application Data\
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Laura G > écrivait :
In this message, < Laura G > wrote:

|| Hi Jim,
||
|| Thanks for the code for the function to get the user's
|| network id. That seems better than prompting the user for
|| it. After the function runs, how do I transfer the
|| username into the macro for the path: c:\Documents and
|| Settings\USERNAME\Application Data\
||

Try this (I think there is a problem with Jim's code as it does not remove
all the NULL characters from the end of the UserName string.... I could be
wrong though!):

'_______________________________________
'Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long

Const NoError = 0 'The Function call was successful

'_______________________________________
Function GetUserName() As String

'Buffer size for the return string.
Const lpnLength As Integer = 255

'Get return buffer space.
Dim status As Integer

'For getting user information.
Dim lpName, lpUserName As String

'Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)

'Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)

'See whether error occurred.
If status = NoError Then
'This line removes the null character. Strings in C are null-
'terminated. Strings in Visual Basic are not null-terminated.
'The null character must be removed from the C strings to be used
'cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
Else

'An error occurred.
MsgBox "Unable to get the name."
End
End If

GetUserName = lpUserName

End Function
'_______________________________________

'_______________________________________
Sub UseName()

Dim MyPath As String
Dim MyName As String

MyName = GetUserName

'Display the name of the person logged on to the machine.
MsgBox "The person logged on this machine is: " & MyName

'Or use it in a path variable
MyPath = "C:\Documents and Settings\" & MyName & "\Application Data\"
MsgBox MyPath

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jim Faraone

Jean-Guy

You are correct....it does not remove it....sorry.

Laura....in the macro where it has the directory path....replace the path
with this:

"c:\Documents and Settings\" & ltrim(rtrim(GetUserNameA())) &
"\Application"

the Ltrim(Rtrim()) should get rid of any null characters.
 
J

Jonathan West

Jim Faraone said:
Jean-Guy

You are correct....it does not remove it....sorry.

Laura....in the macro where it has the directory path....replace the path
with this:

"c:\Documents and Settings\" & ltrim(rtrim(GetUserNameA())) &
"\Application"

the Ltrim(Rtrim()) should get rid of any null characters.

LTrim & RTrim trim spaces, they do not trim nulls.

You need the following function

Function TrimNulls(byVal strIn as String) As String
Dim iNull as Long
iNull = Instr(strIn, Chr$(0))
If iNull = 0 Then
TrimNulls = strIn
Else
TrimNulls = Left$(strIn, iNull - 1)
End If
End Function

It is then used in the line of path as follows

"c:\Documents and Settings\" & TrimNulls(GetUserNameA()) & "\Application"
 

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