Open a file as read only

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
Is it possible to use the workbook open event to open a file as read only?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Like the below:
VBA Code:
Sub test()
    Dim fPath As String
    Dim wb As Workbook

    fPath = "C:\path\to\your\file.xlsx" ' Update with the actual file path
    Set wb = Workbooks.Open(Filename:=fPath, ReadOnly:=True)
    
    ' Perform operations on the read-only workbook if needed
    
    ' Close the workbook
    wb.Close False
End Sub
 
Upvote 0
No. I mean in here:
VBA Code:
Private Sub Workbook_Open()
'if username is not x then open as read only
End Sub
 
Upvote 0
Would you not be better off putting a password on the file with an option to open read only for anyone without the password?
 
Upvote 0
Try...

VBA Code:
Private Sub Workbook_Open()

    If Application.UserName <> "John Smith" Then 'change the user name accordingly
        ThisWorkbook.ChangeFileAccess xlReadOnly
    End If
    
End Sub

Hope this helps!
 
Upvote 0
Solution
Try...

VBA Code:
Private Sub Workbook_Open()

    If Application.UserName <> "John Smith" Then 'change the user name accordingly
        ThisWorkbook.ChangeFileAccess xlReadOnly
    End If
   
End Sub

Hope this helps!

Note that VBA must be enabled in order for the event to occur. For this reason, you may want to adopt the suggestion offered by @Georgiboy instead.
 
Upvote 0

Forum statistics

Threads
1,215,150
Messages
6,123,312
Members
449,094
Latest member
Chestertim

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