Open a Word Document in Read-Only Mode

Ever wanted/needed to open a Word Document, but in read-only mode? Well that’s what Plugging_Away as help with in the Answers Forum

 

A Permanent Solution

Now, Tom van Stiphout suggested an interesting solution, that is to use the SetAttr statement to change the file attribute itself, making the document read-only to all.  This is a great way to protect a document.

If you want the document to only be read-only via your application, but remain editable otherwise, this will not work as you have no means to set it back since you have no event to do so when the user closes the document.

A Onetime Solution

If the intent is to make a document read-only when launched from your application then Word, Excel, … automation is the solution.  Once you review the documentation you will actually see that this is directly an option in the Open methods, it’s just a case of creating a Word instance and then opening the document with a read-only argument set to True.

As such, for Microsoft Word, the code would look like:

'---------------------------------------------------------------------------------------
' Procedure : Word_OpenDoc
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Open a Word document
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding  -> None required
'             Early Binding -> Microsoft Word XX.X Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFile     : Fully qualified path and filename of the Word document to open
' bReadOnly : Whether the document should be opened in read-only more, or not
'
' Usage:
' ~~~~~~
' Call Word_OpenDoc("C:\Temp\Statistics.docx", True) -> Opens in read-only mode
' Call Word_OpenDoc("C:\Temp\Statistics.docx") -> Opens in normal mode
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2022-02-09              Initial Public Release
'---------------------------------------------------------------------------------------
Public Sub Word_OpenDoc(ByVal sFile As String, Optional bReadOnly As Boolean = False)
    On Error GoTo Error_Handler
    #Const Word_EarlyBind = False    'True => Early Binding / False => Late Binding
    #If Word_EarlyBind = True Then
        Dim oWord             As Word.Application
        Dim oDoc              As Word.Document
    #Else
        Dim oWord             As Object
        Dim oDoc              As Object
        '    Const wdNormalView = 1
        Const wdPrintView = 3
        '    Const wdReadingView = 7
    #End If

    Set oWord = CreateObject("Word.Application")
    Set oDoc = oWord.Documents.Open(sFile, , bReadOnly)
    oWord.Visible = True
    oWord.ActiveWindow.View.Type = wdPrintView

Error_Handler_Exit:
    On Error Resume Next
    If Not oDoc Is Nothing Then Set oDoc = Nothing
    If Not oWord Is Nothing Then Set oWord = Nothing
    Exit Sub

Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: Word_OpenDoc" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Sub

Simply copy/paste the procedure above into a standard module and then you can open any document with a single line of code.

Usage Examples

Open A Document in Read-Only Mode

Call Word_OpenDoc("C:\Temp\Statistics.docx", True)

Open A Document in Normal Mode

Call Word_OpenDoc("C:\Temp\Statistics.docx")

Or

Call Word_OpenDoc("C:\Temp\Statistics.docx", False)

Code Features

This code

  • can be used as both Early or Late Binding, the choice is yours and set by changing the value of the Word_EarlyBind constant.
  • is architecture/bitness independent (works in both 32 and 64-bit installations)
  • is application independent (should work in any VBA applications – Access, Excel, Outlook, PowerPoint, …)

A Few Resources on the Subject