VBA Code to Check for Domain Match and Open Excel File Security

jgarza0422

New Member
Joined
Apr 8, 2019
Messages
9
I am trying to put a security check in some of our excel files in order to be used within our companies domain. Then if it fails do not open the file and if the check is valid then open excel file.

Here is what I got started, any help would be appreciated.

Private Sub Workbook_Open()


Dim chkUserDomain As String
Dim strUserDomain As String


chkUserDomain = "Test"
strUserDomain = Environ$("UserDomain")


If chkUserDomain <> strUserDomain Then
MsgBox "You can not open this file!"

Else


OPEN CURRENT EXCEL FILE????


End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try this
Code:
Private Sub Workbook_Open()



Dim chkUserDomain As String
Dim strUserDomain As String




chkUserDomain = "Test"
strUserDomain = Environ$("UserDomain")




If chkUserDomain <> strUserDomain Then
         
MsgBox "You can not open this file!"
         
ThisWorkbook.Close


End If


End Sub
 
Upvote 0
I just realized that you would be better off just placing a password on your workbook because, this macro won't run if the user has macros disabled
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top