I have a workbook that has multiple authors, each of them should only be able to see their own worksheet and the primary sheet. I have a limited understanding of VBA from past coding but if hiding a sheet by either email they are logged into, by their outlook contact info, or the author is possible while still sharing the same file, I'd love the how-to. I did a bit of research already and understand that I can get the username and check by that, but asking everyone for their PC username is rough.
CodePudding user response:
VBA can be easily hacked by somebody skilled... If people using the workbook in discussion are not so skilled and you intend protect the VBA project, you can try using a function able to return what you need. It will work only if the user has Outlook installed and its default/primary user account is the one you know and use in your code. Just for the sake of playing with VBA, try the next function:
Function getActiveOutlookAccount() As String
Dim Outl As Object, OutMail As Object, boolAlreadyOp As Boolean
On Error Resume Next
Set Outl = GetObject(, "Outlook.Application")
On Error GoTo 0
If Outl Is Nothing Then
Set Outl = CreateObject("Outlook.Application")
Else
boolAlreadyOp = True
End If
Set OutMail = Outl.CreateItem(0) 'necessary to expose Outlook namespace...
getActiveOutlookAccount = Outl.GetNamespace("MAPI").CurrentUser.Address
If Not boolAlreadyOp Then Outl.Quit: Set Outl = Nothing 'quit Outlook if not already open before running the code...
End Function
It can be tested using the next sub:
Sub getAccount()
Dim currentAcc As String
currentAcc = getActiveOutlookAccount
Debug.Print currentAcc
'use it to set access to specific worksheets...
End Sub