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.
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
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 _ ActiveDocument.Save 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
Dimstatements to declare
variables, or storage slots for data used in the macro. The format is
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
strDocNamevariable receives the document's name (without the path). The
strDocFullNamevariable receives the document's name and path. The
strTitlevariable receives the text to be displayed in the titlebar of each message box. The
intPagevariable 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.)
Ifstatement 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 Ifstatement just before the
End Substatement, and the macro ends without taking any action.
Provided the user has clicked the Yes button in the first message box, the next
Ifstatement 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
Dialogscollection. 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 Substatement exits the macro. Otherwise, the
strDocFullName = ActiveDocument.FullNamestatement assigns the document's name and path (now that it has one) to the
Ifstatement checks whether the document contains unsaved changes (
If Not ActiveDocument.Savedtranslates 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.Savestatement saves the changes.
After all those preliminaries, it's time for action. The
ActiveDocument.Close SaveChanges:=wdDoNotSaveChangesstatement closes the document without saving changes. (If the user chose to save unsaved changes, they've already been saved.) The
Documents.Openstatement then uses the
strDocFullNamevariable to open the document again, this time as read-only. The
Selection.GoTostatement displays the page whose number is stored in the
intPagevariablethat is, the page that was displayed when the macro was run.