Solved

Set a MS Word document to read only via VBA

Posted on 2013-06-13
6
1,670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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