Share via

READONLY-VBA

Anonymous
2013-01-24T07:18:42+00:00

HI,

IS IT POSSIBLE TO MAKE A FILE READ ONLY THROUGH VBA CODING?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2013-01-24T16:52:29+00:00

HI,

IS IT POSSIBLE TO MAKE A FILE READ ONLY THROUGH VBA CODING?

You can use VBA's SetAttr statement to do that. Look it up in the VB editor's help files. Here is an online link that gives a brief description of the statement's syntax as well...

http://vbadud.blogspot.com/2007/05/changing-file-attributes.html

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Anonymous
2013-01-24T16:18:22+00:00

Hi,

try this sample..

(save in existing Path, new name: BOOK1 ReadOnly.xlsm)

SubSaveAsReadOnly()

Dim myPath

myPath = ThisWorkbook.Path & "\BOOK1 ReadOnly.xlsm"

Application.DisplayAlerts = False

ActiveWorkbook.SaveAs Filename:=myPath, FileFormat:=xlNormal, ReadOnlyRecommended:=True

Application.DisplayAlerts = True

End Sub

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-05-08T12:46:13+00:00

    Thanks for sharing!

    Your line of code works fine. For general use I would recommend something like this:

    ActiveWorkbook.SaveAs Filename:=Path & name1 & "_" & mydate & ".xlsx", FileFormat:=51

    myfilename = ActiveWorkbook.FullName

    SetAttr myfilename, vbReadOnly

    name1, a string variable, is taken from, Range("C1") for example

    myfilename is another string variable

    Path is a string variable, eg. "C:\users\test"

    Dinesh K Takyar

    www.exceltrainingvideos.com

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-01-24T18:19:02+00:00

    Use code like

    SetAttr "G:\A\Book1.xlsm", vbReadOnly

    This will make Book1.xlsm read-only.

    Was this answer helpful?

    0 comments No comments