IIRC, Application.Username need not be the same as Bob's suggestion.
Application.Username is set when you install Office and can be anything. It
may well be the person who installed the software, rather than the current
user.
Instead, use:
Environ("Username")
Whilst this can also be changed by someone playing with the Environment
variables, it is less likely.
There's always the API route also:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal
lpBuffer As String, nSize As Long) As Long
Similar arguments apply to "Computername".
NickHK
"vbapro" wrote in message
...
Good Morning!
You have done almost everything correct. I must have written not enough
clear. The second part of the code must have been inserted into a sheet's
module; they are generally called in VBA Editor as "Sheet1(Sheet1)" and so
on.
Moreover, Bob Phillips has given a witty solution, which is shorter; so
there is a new example with use of a part of his tip.
Put this code into the worksheet's module "Sheet1(Sheet1)":
Private Sub Worksheet_Activate()
Dim LocalHostName As String ' the computer's domain name
Dim UserName As String ' the name of a user
Dim WorkSheetName As String ' the name of a sheet
LocalHostName = Environ("Computername")
UserName = Application.UserName
WorkSheetName = ThisWorkbook.ActiveSheet.Name
MsgBox "Computer name is:" & vbLf & vbTab & """" & LocalHostName &
""""
& vbLf & vbLf _
& "User name is:" & vbLf & vbTab & """" & UserName & """" & vbLf &
vbLf _
& "You are trying to access the worksheet:" & vbLf & vbTab & """"
&
WorkSheetName & """"
End Sub
Here is an exaple to download:
http://www.vbapro.ru/downloads/CheckOnNameAndHost.xls
Vladimir
"Smiley" wrote:
Hi there,
I must be doing something very silly. I just cannot get it works.
The following is what I have done.
Open a new workbook and on sheet 1 then I go to Tools -- Macro --
Visual
Basic Editor
Then on Insert -- Module, I copied
Public Declare Function gethostname Lib "wsock32.dll" (ByVal Name As
String,
ByVal namelen As Long) As Long
Then again on Module, I copied the Private Sub Worksheet_activate
Close the workbook and reopen it, I got a popup which gave me options of
disable Marco, Enable Marco and More Info. I clicked on the enable Marco
but
nothing happened.
Am I miles out ?
When I opened up the workbook, I cannot find the public declare function
or
the private sub. Where can I found them. Also shall I using Class module
? I
am very confuse on the module, class modules etc. Would you nudge me in
the
right direction. Many thanks.
Smiley
"vbapro" wrote in message
...
Insert the following part of code into a general code module:
==============
Public Declare Function gethostname Lib "wsock32.dll" (ByVal Name As
String,
ByVal namelen As Long) As Long
==============
and insert this part into the module of any worksheet, this is just an
example:
==============
Private Sub Worksheet_Activate()
Dim LocalHostName As String ' the computer's domain name
Dim UserName As String ' the name of a user
Dim WorkSheetName As String ' the name of a sheet
LocalHostName = Space(256)
gethostname LocalHostName, 256
LocalHostName = Left(LocalHostName, InStr(LocalHostName,
vbNullChar) -
1)
UserName = Application.UserName
WorkSheetName = ThisWorkbook.ActiveSheet.Name
MsgBox "Computer name is:" & vbLf & vbTab & """" & LocalHostName &
""""
& vbLf & vbLf _
& "User name is:" & vbLf & vbTab & """" & UserName & """" &
vbLf &
vbLf _
& "You are trying to access the worksheet:" & vbLf & vbTab &
""""
&
WorkSheetName & """"
End Sub
==============
Now, if you try to activate the worksheet with the code an appropriate
message appears. This is an example with all necessary functions,
which
help
you to check on the computer name and user name.
Regards, Vladimir
"Smiley" wrote:
Good afternoon,
Is it possible to have a checking on the computer name and user name
when
certain worksheets are being accessed. i.e. I only want certain work
only
been done by certain person and on certain machine.
If that is possible where can I place this security check and how.
Some
coding would be very helpful as I am totally new to programming.
Regards,
Smiley