Opening Workbooks as Read Only

mcgrathm

New Member
Joined
Mar 18, 2002
Messages
3
I want to open a workbook as read-only depending on which user is opening the file. I know how to test for the user but I cannot seem to get the syntax for Workbooks.Open quite right. Can anybody help me please
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Tough one. I would say that you will be better off password-protecting it for modifications. Hit File-Save as and choose Tools-General options. Put a password in to modify and give the password only to your selected users.
 
Upvote 0
Hi,

This can be done by placing some code in the Workbook_Open event code. Right click the lower of the 2 Excel icons at the top left of the Excel screen and choose View Code. Alternatvely, open the VB editor, click Ctrl R to open the project explorer. Then expand your workbook if it's not already expanded and double click the ThisWorkbook icon.

There are 2 names you could check. The first, and most unreliable is the UserName given by Excel. This is the one that you type into Tools, Options, General. For that, use this code:-

Code:
Private Sub Workbook_Open()
If Application.UserName <> "dk" Then
    Application.DisplayAlerts = False
    ThisWorkbook.ChangeFileAccess (xlReadOnly)
    Application.DisplayAlerts = True
End If
End Sub

The second method is to check the Windows logon name. For that use something like this:-

Code:
'GetUserName code is from here:-
'http://www.vbapi.com/ref/g/getusername.html

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private Function WindowsUserName()
' Display the name of the user currently logged on.
Dim username As String  ' receives name of the user
Dim slength As Long  ' length of the string
Dim retval As Long  ' return value

' Create room in the buffer to receive the returned string.
username = Space(255)  ' room for 255 characters
slength = 255  ' initialize the size of the string
' Get the user's name and display it.
retval = GetUserName(username, slength)  ' slength is now the length of the returned string
WindowsUserName = Left(username, slength - 1)  ' extract the returned info from the buffer
' (We subtracted one because we don't want the null character in the trimmed string.)

End Function

Private Sub Workbook_Open()
If WindowsUserName = "nothim" Or WindowsUserName = "nother" Then
    Application.DisplayAlerts = False
    ThisWorkbook.ChangeFileAccess xlReadOnly
    Application.DisplayAlerts = True
End If
End Sub

HTH,

Dan
 
Upvote 0
Dan....thats good......

Forgot all about the ChangeFileAccess bit
I was actually looking @ a diff method..
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,172
Members
448,870
Latest member
max_pedreira

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