Solved

Set a MS Word document to read only via VBA

Posted on 2013-06-13
6
1,589 Views
Last Modified: 2013-06-16
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
Comment
Question by:thenelson
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39245111
0
 
LVL 39

Author Comment

by:thenelson
ID: 39245934
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
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39247341
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 39

Author Comment

by:thenelson
ID: 39247444
"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
 
LVL 6

Accepted Solution

by:
BurundiLapp earned 500 total points
ID: 39247530
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
 
LVL 39

Author Closing Comment

by:thenelson
ID: 39251897
Thanks!
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question