S Oberlander
Board Regular
- Joined
- Nov 25, 2020
- Messages
- 147
- Office Version
- 365
- Platform
- Windows
Is it possible to use the workbook open event to open a file as read only?
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
Private Sub Workbook_Open()
'if username is not x then open as read only
End Sub
Private Sub Workbook_Open()
If Application.UserName <> "John Smith" Then 'change the user name accordingly
ThisWorkbook.ChangeFileAccess xlReadOnly
End If
End Sub
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!
its an idea, but won't work in my situationWould you not be better off putting a password on the file with an option to open read only for anyone without the password?