Set ReadOnly using VBA

Jzog

New Member
Joined
Jan 8, 2013
Messages
4
I am trying to save a workbook read only in a folder selected by the user. I am get the code for allowing the user to select a folder; but when it comes to the setting read only attribute, this is not working. It appears to work, but then when I try to have another user open the file, they are able to open the file, make changes to the file, and save the file with the same name. At not time do they get a "ReadOnly" message. Below is my code, can anyone help, please?

Thank you!:confused:

Code:
Sub GetFolder()
Dim fldr As FileDialog
Dim sItem As String
Dim strPwd As String
Dim strPath As String
Dim FileName As String
'strPwd = InputBox("Enter the password")
strPath = "M:\YM07\Group\d031\Account\NDL Journal Entries\2013\"

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
Set fldr = Nothing
ActiveWorkbook.SaveAs
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
ChDir "M:\YM07\Group\d031\Account\NDL Journal Entries\2013"

End Sub

[code]
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try something like this when saving the file...
Code:
ActiveWorkbook.SaveAs strPath & ActiveWorkbook.Name, ReadOnlyRecommended:=True
SetAttr strPath & ActiveWorkbook.Name, vbReadOnly

The .ChangeFileAccess method doesn't change the Read Only property of the saved file.
 
Upvote 0
check out the SetAttr command:
SetAttr Statement


Sets attribute information for a file.

Syntax

SetAttr pathname, attributes

The SetAttr statement syntax has these named arguments:

Part Description
pathname Required. String expression that specifies a file name — may include directory or folder, and drive.
attributes Required. Constant or numeric expression, whose sum specifies file attributes.



Settings

The attributes argument settings are:

Constant Value Description
vbNormal 0 Normal (default).
vbReadOnly 1 Read-only.
vbHidden 2 Hidden.
vbSystem 4 System file. Not available on the Macintosh.
vbArchive 32 File has changed since last backup.
vbAlias 64 Specified file name is an alias. Available only on the Macintosh.



Note These constants are specified by Visual Basic for Applications. The names can be used anywhere in your code in place of the actual values.

Remarks

A run-time error occurs if you try to set the attributes of an open file.
 
Upvote 0
SOLVED: Set ReadOnly using VBA

Thank you both for your responses.

AlphaFrog this solution worked for me. Thanks again! ;)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

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