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