Opening as read-only

vbacoder

Active Member
Joined
Jul 7, 2007
Messages
354
Hi everyone,

Just a quick question: how do I modify the open worksheet command in VB to open a file as read-only?

Many thanks,

vcoder
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
This sets the active Workbook to Read-Only or Not.

Sub myReadOnly()
'Sheet module code, like: Sheet1.
Dim fs, f, r

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fs.GetFileName(ActiveWorkbook.FullName))

'Note: "1" is the Read Only code!
If f.Attributes And 1 Then
r = MsgBox("The Read Only bit is set, do you want to clear it?", vbYesNo, "Set/Clear Read Only Bit")

If r = vbYes Then
f.Attributes = f.Attributes - 1
MsgBox "Read Only bit is cleared."
Else
MsgBox "Read Only bit remains set."
End If

Else

r = MsgBox("The Read Only bit is not set. Do you want to set it?", vbYesNo, "Set/Clear Read Only Bit")

If r = vbYes Then
f.Attributes = f.Attributes + 1
MsgBox "Read Only bit is set."
Else
MsgBox "Read Only bit remains clear."
End If
End If
End Sub




This is the key to setting it read only:


Dim fs, f

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fs.GetFileName(ActiveWorkbook.FullName))

f.Attributes = f.Attributes + 1
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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