C
cindy
Hi-
I need a little help to get this macro to run
automatically when the the document is opened.
I got this macro from a newsgroup/web and it works to
grab the userID - BUT I have to run it manually.
Can I add something to it, so it will run when they open
the file. thanks in advance for your help!!!!
Here's the macro:
*******************************
'Macro to grab Network ID and set it in Word
Option Explicit
'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 Long = 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)
End If
'Display the name of the person logged on to the machine.
GetUserName = lpUserName
End Function
'Call it with this instead of original:
Sub SetWordUserName()
Application.UserName = GetUserName
End Sub
thanks again!!!! Cindy
I need a little help to get this macro to run
automatically when the the document is opened.
I got this macro from a newsgroup/web and it works to
grab the userID - BUT I have to run it manually.
Can I add something to it, so it will run when they open
the file. thanks in advance for your help!!!!
Here's the macro:
*******************************
'Macro to grab Network ID and set it in Word
Option Explicit
'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 Long = 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)
End If
'Display the name of the person logged on to the machine.
GetUserName = lpUserName
End Function
'Call it with this instead of original:
Sub SetWordUserName()
Application.UserName = GetUserName
End Sub
thanks again!!!! Cindy