Set a MS Word document to read only via VBA

Can a MS Word document be set to read only via VBA?  ActiveDocument.ReadOnly is read only. For example:
If Not IsDate(ActiveDocument.Bookmarks("EndTime").Range.Text) Then
    MsgBox "The end time of this report has not been set." & vbCrLf & "Setting the report to read only."
    Set ActiveDocument.ReadOnly = True  '(pseudo code)
End If

Open in new window

LVL 39
thenelsonAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
BurundiLappConnect With a Mentor Commented:
You could use the shell command to run icacls and deny the write attribute to everyone for that particular file, e.g:
icacls test1.doc /deny Everyone:(w)

Open in new window


How do you intend to unset it?  Using another macro?

The code to reset the file back to it's inherited permissions would be:

icacls test1.doc /reset

Open in new window


The VBA would look like

shlcmd$ = "icacls " & ActiveDocument.FullName & " /deny everyone:(w)"
Shell shlcmd$

Open in new window

0
 
thenelsonAuthor Commented:
Nice find and try but it doesn't help.  ActiveDocument.WritePassword causes the document to require a password any time it is saved. It doesn't make the document read only conditionally.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
BurundiLappCommented:
I'm not sure what you are trying to achieve, are you trying to set the NTFS permissions on the word document to be 'read only' but from within the word document itself.
0
 
thenelsonAuthor Commented:
"are you trying to set the NTFS permissions on the word document to be 'read only' but from within the word document itself."
Yes, or some other way to prevent the user from making changes to the document and then saving it.
0
 
thenelsonAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.