MS Word

Switch the Active Document to Read-Only Status

The Problem:

I receive lots of documents that I need to read but that I'm not supposed to change. I know I could open them as read-only from the Open dialog box by using the drop-down on the Open button, but I usually open them directly from email or from Explorer, so I don't have that choice. What I'd like is a box to check that tells Word I want to handle a document as "read only" after I've already opened it.

The Solution:

As you say, there's no simple checkboxor other controlto switch an open document to read-only status. But you can create a macro that does this well enough (see Example 8-4). Also, from Windows Explorer, you can right-click a Word document and choose "Open as Read-Only" from the shortcut menu.

Example 8-4 probably looks impenetrable, but once you get the hang of the If... Then statements, it's pretty straightforward. Here's what's happening:

Macro to switch the active document to read-only status

Example 8-4
 Sub Reopen_Active_Document_As_Read_Only()
    'close the active document and reopen it as read-only
    'store the page number and return to it when the document is reopened
    Dim strDocName As String
    Dim strDocFullName As String
    Dim strTitle As String
    Dim intPage As Integer
    strDocName = ActiveDocument.Name
    strDocFullName = ActiveDocument.FullName
    strTitle = "Reopen Document As Read-Only"
    intPage = Selection.Information(wdActiveEndPageNumber)
    If MsgBox(Prompt:="Close " & strDocName & " and reopen as read-only?", _
         Buttons:=vbYesNo + vbQuestion, Title:=strTitle) = vbYes Then
         If ActiveDocument.Path = "" Then
            MsgBox Prompt:="This document has never been saved." & vbCr & vbCr _
               & "Please save it now.", _
               Buttons:=vbOKOnly + vbExclamation, Title:=strTitle
         If Dialogs(wdDialogFileSaveAs).Show = 0 Then
             Exit Sub
    End If
    strDocFullName = ActiveDocument.FullName
  End If
  If Not ActiveDocument.Saved Then
      If MsgBox(Prompt:="The document contains unsaved changes." & vbCr _
           & vbCr & "Click Yes to save the changes; " & _
           "click No to discard the changes.", Buttons:=vbYesNo + vbQuestion, _
           Title:=strTitle) = vbYes Then _
  End If
  ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
  Documents.Open FileName:=strDocFullName, ReadOnly:=True
  Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=intPage
  End If
End Sub
  • The first few lines are comments explaining what the macro does.

  • The second group of lines uses Dim statements to declare variables, or storage slots for data used in the macro. The format is Dim variablename As variabletype. The first three declarations create String variables, which are variables that can hold text characters. The fifth declaration creates an Integer variable, which is a variable that can contain only a whole number (either positive or negative).

  • The third group of lines assigns information to the variables. The strDocName variable receives the document's name (without the path). The strDocFullName variable receives the document's name and path. The strTitle variable receives the text to be displayed in the titlebar of each message box. The intPage variable gets the page number of the end of the current selection. (This will be used for the macro to display this page again after reopening the document.)

  • The first If statement displays a message box to confirm that the user wants to run the macro. If the user clicks the Yes button, returning the value vbYes, the rest of the macro runs; if not, execution branches to the End If statement just before the End Sub statement, and the macro ends without taking any action.

  • Provided the user has clicked the Yes button in the first message box, the next If statement checks whether the active document's path is an empty string ("" double quotation marks with no characters between them). If so, that means the document has never been saved, so the macro displays a notification message box (with just an OK button) followed by the Save As dialog box, which is the object named wdDialogFileSaveAs in the Dialogs collection. If the value returned by the Save As dialog box is 0, the user has clicked the Cancel button rather than saving the document, so the Exit Sub statement exits the macro. Otherwise, the strDocFullName = ActiveDocument.FullName statement assigns the document's name and path (now that it has one) to the strDocFullName variable.

  • The next If statement checks whether the document contains unsaved changes (If Not ActiveDocument.Saved translates to "if the active document has not been saved") and, if so, displays a message box asking the user to click the Yes button to save the changes or the No button to discard the changes. If the user clicks the Yes button, the ActiveDocument.Save statement saves the changes.

  • After all those preliminaries, it's time for action. The ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges statement closes the document without saving changes. (If the user chose to save unsaved changes, they've already been saved.) The Documents.Open statement then uses the strDocFullName variable to open the document again, this time as read-only. The Selection.GoTo statement displays the page whose number is stored in the intPage variablethat is, the page that was displayed when the macro was run.

by BrainBellupdated