2
A spreadsheet available on the network can be prevented from running a macro if the machine is not "authorized"?
I need to block certain users or PC’s using the MAC address for example.
2
A spreadsheet available on the network can be prevented from running a macro if the machine is not "authorized"?
I need to block certain users or PC’s using the MAC address for example.
2
If you wish to identify the user, there are two ways. You can use registered user identification in Excel itself by simply accessing the property Application.UserName
. Or, you can access the registered user name in the operating system account (login), using the following code:
Private Declare Function Get_User_Name Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function GetUserName() As String
Dim sName As String * 25
Get_User_Name sName, 25
GetUserName = Left(sName, InStr(sName, Chr(0)) - 1)
End Function
If you wish to identify the MAC Address you can use the following code (remembering that there is a MAC Address for each network adapter on the computer, then this example code returns a string with all existing ones separated by a "/" bar - you can change it to return a string array if you want):
Function GetMACAddr()
Dim sComputer As String
Dim oWMIService As Object
Dim oAdapters As Object
Dim oAdapter As Object
Dim sRet As String
sComputer = "."
sRet = ""
Set oWMIService = GetObject("winmgmts:" & "!\\" & sComputer & "\root\cimv2")
Set oAdapters = oWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each oAdapter In oAdapters
If Len(sRet) > 0 Then
sRet = sRet + "/" + oAdapter.MACAddress
Else
sRet = oAdapter.MACAddress
End If
Next oAdapter
GetMACAddr = sRet
End Function
Now, if these functions can be used to "block" the access or execution of the spreadsheet, this is another problem. There are some important things to consider:
Original sources of the codes:
Browser other questions tagged excel vba networks macro
You are not signed in. Login or sign up in order to post.
Luiz, thank you again for that very well prepared reply. Regarding the observations, the macro runs when opening the worksheet, the user must have access to information and others will be restricted ( depending on the user). But I appreciate the directions.
– h1k3r
Yes, I get it. It turns out that the macro is not executed automatically only because it was conditioned to the spreadsheet opening event. Every time a user opens a file with macros in Excel, for safety Excel questions whether the execution should be enabled. I suggest you test what happens on your system if the user simply says "no" to this initial question. :)
– Luiz Vieira
I get it. Perfect. (Y)
– h1k3r