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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,215,487
Messages
6,125,082
Members
449,205
Latest member
Healthydogs

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