How to make any file read-only using VBA?



VBA has in-built function SetAttr to change attribute of a file or folder. This Function is classified as File or Directory function. Below is the syntax of SetAttr and it takes two parameters.

        Syntax: SetAttr (FilePath, Attribute)

FilePath is the fully qualified path of the file and Attribute is the parameter of a file which you wish to change. In this example we are going to change attribute of the file ‘Sample.xlsx’ from normal to read-only.

Example:

Sub ReadOnly()
Dim s as String

S = C:\LM10\Sample.xlsx
SetAttr (S, vbReadOnly)

End Sub



Share:

0 comments:

Post a Comment