Thanks to Skip for getting my mind going on this. I ended up being able to set up the shared XLA file to recognize my username. Then I wanted to ensure that if I were hit by a bus, someone else could edit it without having to log in as me. I found some code that checks against AD Groups, and that's helping a ton along those lines. So I have 2 checks in place for now, may modify later. But with this setup, everybody else opens the XLA file as read-only.
Initial idea of how to use Username: couple of ideas posted:
For AD Groups to help account for hit by bus events:
So now, my combined solution (for the time being), my code in the "ThisWorkbook" object of the XLA file is:
[CODE VBA]Private Sub Workbook_Open()
SetAsReadOnly
End Sub
Sub SetAsReadOnly()
' Test for PC User Name
Dim strUser As String
Dim wbRCCustom As Workbook
Set wbRCCustom = ThisWorkbook
strUser = Environ("USERNAME")
' MsgBox strUser
' Set Read only File Access for each Office's specific version
If UserIsInGroup("Domain Admins") Then
Msgbox "Current user is a Domain Administrator"
If wbRCCustom.ReadOnly Then _
wbRCCustom.ChangeFileAccess Mode:=xlReadWrite ', WritePassword:="admin"
ElseIf strUser = "MyWindowsUserName" Then
' Msgbox "User is OK, so OK to edit"
If wbRCCustom.ReadOnly Then _
wbRCCustom.ChangeFileAccess Mode:=xlReadWrite ', WritePassword:="admin"
Else ' Limit Access
If Not wbRCCustom.ReadOnly Then _
wbRCCustom.ChangeFileAccess Mode:=xlReadOnly ', WritePassword:="admin"
End If
End Sub[/CODE]
Then in a module, I just kept the original code from 2nd link above:
Code:
Public Function UserIsInGroup(GroupName As String, _
Optional Username As String, _
Optional Domain As String) As Boolean
'On Error Resume Next
' Returns TRUE if the user is in the named NT Group.
' If user name is omitted, current logged-in user's login name is assumed.
' If domain is omitted, current logged-in user's domain is assumed.
' User name can be submitted in the form 'myDomain/MyName'
' (this will run slightly faster)
' Does not raise errors for unknown user.
'
' Sample Usage: UserIsInGroup( "Domain Users")
Dim strUsername As String
Dim objGroup As Object
Dim objUser As Object
Dim objNetwork As Object
UserIsInGroup = False
If Username = "" Then
Set objNetwork = CreateObject("WScript.Network")
strUsername = objNetwork.UserDomain & "/" & objNetwork.Username
Else
strUsername = Username
End If
strUsername = Replace(strUsername, "\", "/")
If InStr(strUsername, "/") Then
' No action: Domain has already been supplied in the user name
Else
If Domain = "" Then
Set objNetwork = CreateObject("WScript.Network")
Domain = objNetwork.UserDomain
End If
strUsername = Domain & "/" & strUsername
End If
Set objUser = GetObject("WinNT://" & strUsername & ",user")
If objUser Is Nothing Then
' Insert error-handler here if you want to report an unknown user name
Else
For Each objGroup In objUser.Groups
'Debug.Print objGroup.Name
If GroupName = objGroup.Name Then
UserIsInGroup = True
Exit For
End If
Next objGroup
End If
Set objNetwork = Nothing
Set objGroup = Nothing
Set objUser = Nothing
End Function
The only other possibility is to allow someone to open the file as ReadWrite who is not an admin as a one-off fix. However, that would happen so rarely, that it's probably not worth bothering with. Also, there are only 3 people who know or can actively find the password for the XLA file's VBA anyway. That's me, one manager, and the other IT guy (who never has touched any of the Excel stuff from day one, and has no desire whatsoever to ever touch it). So in reality, I think this solves the problem. I've tested the code with a couple of people in a sample file. The Lord willing, tomorrow morning, I'll add it to the production code, so I don't have to be concerned with whether someone beat me to the punch opening an Excel file.
"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57