+ Reply to Thread
Results 1 to 7 of 7

Workbook_Open Check UserName

  1. #1
    Forum Contributor
    Join Date
    08-15-2006
    Location
    USA
    MS-Off Ver
    Office 365, 64 bit
    Posts
    372

    Workbook_Open Check UserName

    I currently have a workbook that when it opens, a message box appears warning them that they need to have a certain role to continue to open and view the data. However, they can still click OK instead of Cancel to proceed as nothing stops them from doing it (in the event the workbook get's forwarded).

    Is there a piece of code that will check if the "UserName" matches a list of usernames in a table in a hidden tab and if located, allow them to continue to as normal and view the aforementioned message box? If the "UserName" doesn't match any of the usernames, than they will get a different message such as:

    "You are not authorized to open or view this file. To get access, please contact the administrator." {the workbook will than close)

  2. #2
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    423

    Re: Workbook_Open Check UserName

    This should do it for you:
    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Workbook_Open Check UserName

    While it is possible to have code check the username and close, the end user can still open and browse the workbook if macros are not enabled.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  4. #4
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Workbook_Open Check UserName

    You will need to hide the sheets on workbook before close. xlSheetVeryHidden

    And then unhide them if user is authorized

  5. #5
    Forum Contributor
    Join Date
    08-15-2006
    Location
    USA
    MS-Off Ver
    Office 365, 64 bit
    Posts
    372

    Re: Workbook_Open Check UserName

    I tested it with my username and works fine. Quick question:

    Will this code scan Column A to match the username and if found allow them to open the workbook? Or is it only for the A1 cell? Thx in advance...

  6. #6
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Workbook_Open Check UserName

    As mc84excel mentioned, if they do not have macro enabled, your security will not work.

    This will help on that.

    ' Event handler before closing application
    Please Login or Register  to view this content.
    And unhide them in the workbook open.

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Workbook_Open Check UserName

    Quote Originally Posted by nordicdust View Post
    As mc84excel mentioned, if they do not have macro enabled, your security will not work.

    This will help on that.

    ' Event handler before closing application
    Please Login or Register  to view this content.
    And unhide them in the workbook open.
    • If an approved user doesn't choose to Save on workbook close then the workbook sheets will be visible to the next person who opens the workbook with macros disabled.
    • If Excel crashes on an approved user then then it is possible that the same situation will occur.


    While Excel security is inherently flimsy, there are actions that can be taken to increase the protection of your project from casual snoopers.
    For instance - hijack the Save event and replace it with your own custom version that will apply the xlVeryHidden so that the workbook is always saved with the sheets hidden. By doing this, the protection wont break if the user forgets to save or if their Excel session crashes. Nor does this protection depend on the next end user having macros enabled when opening the workbook.
    This custom save event is part of a concept known as 'Force user to enable macros'. A quick online search should return several different versions of this code (with varying degrees of quality). Pick one you like.

    If you have not already done so, you will need to password protect your VBA project to prevent others from viewing the code.


    Additional comments:
    1. While the 'force enable macros' concept will increase the protection of your workbook, don't ever assume that this has made you secure. You cant prevent a skilled Excel user from getting in if they really wanted to.
    2. If you want to increase your security, I don't recommend leaving the usernames in a workbook sheet. VeryHidden or not. It would be more safe if all usernames were hardcoded into the VBA project instead. (Again, doing this will help but it will never be perfectly safe. VBA protection can be bypassed too)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Check multiple criteria Workbook_Open event
    By 323428 in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 08-01-2017, 02:24 PM
  2. [SOLVED] Check Application Username then open read only if not on list
    By alimsab in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-27-2015, 06:24 AM
  3. Replies: 1
    Last Post: 04-26-2015, 09:02 PM
  4. [SOLVED] Check list of names against application username for authorisation
    By MIFF3436 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2014, 06:24 PM
  5. [SOLVED] Username and Password Form - Show Username in Sheet
    By ryan180 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2013, 07:06 PM
  6. How to check UserName - EmailAddress
    By maomao5183 in forum Excel General
    Replies: 1
    Last Post: 11-19-2010, 12:06 PM
  7. check username on file start up
    By tinkerbelle in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2009, 02:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1