Solved

Set a MS Word document to read only via VBA

Posted on 2013-06-13
6
1,526 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embedded links don't work in converted PDF file 8 67
Word 2010 mail merge 3 60
Saving document in Word 2007/2010 format using MS Word 2013 6 40
Oart.dll 2 44
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now