Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1837
  • Last Modified:

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

0
thenelson
Asked:
thenelson
  • 3
  • 3
1 Solution
 
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
 
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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
 
BurundiLappCommented:
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:
Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now