Private Sub Workbook_Open()'06JUL2020
'Enforces document control.'Only runs when the document is first opened.
'Enable eventsApplication.EnableEvents = True
'Update loglog.logSheet ("open")
'SettingsDim MAX_NUMBER_OF_USERS As LongMAX_NUMBER_OF_USERS = 20
Dim MAX_DOCUMENT_CONTROL_FIELDS As LongMAX_DOCUMENT_CONTROL_FIELDS = 10
'VariablesDim x As IntegerDim userlistCol As LongDim enableDocument As BooleanDim ws As WorksheetDim userName As StringDim documentControlSheet As Worksheet
Set documentControlSheet = ActiveWorkbook.Sheets("DocumentControls")
'Get usernameuserName = Environ$("username")userName = LCase(userName)
'Loop through the columns looking for the userlistFor x = 1 To MAX_DOCUMENT_CONTROL_FIELDS If (documentControlSheet.Cells(1, x).Value = "userlist") Then userlistCol = x x = MAX_DOCUMENT_CONTROL_FIELDS End IfNext
'Loop through userlist to see if the document should be enabledenableDocument = FalseFor x = 1 To MAX_NUMBER_OF_USERS If (userName = documentControlSheet.Cells(x, userlistCol).Value) Then enableDocument = True End IfNext
'Document controlIf (enableDocument) Then 'Enable the document For Each ws In Worksheets If ( _ ws.name = "File Disabled" Or _ ws.name = "DocumentControls" Or _ ws.name = "CodeTable" Or _ ws.name = "ValidationLists" Or _ ws.name = "log" _ ) Then ws.Visible = xlSheetVeryHidden Else ws.Visible = xlSheetVisible End If Next MsgBox ("Hello " & userName & "! Enjoy the document. :)")Else 'Disable the document For Each ws In Worksheets If (ws.name = "File Disabled") Then ws.Visible = xlSheetVisible Else ws.Visible = xlSheetVeryHidden End If Next MsgBox ("Hello " & userName & ", this document has been disabled.")End If
'Fully enable document if user is myselfIf userName = "myusername" Then For Each ws In Worksheets ws.Visible = xlSheetVisible Next MsgBox ("Hello " & userName & "! Enjoy the document. :)")End If
End Sub