databasedev.co.uk - database solutions and downloads for microsoft access

Microsoft Access and Opening Word Documents

How to open Microsoft Word documents from a Microsoft Access Form:

Question » From my Microsoft Access form I would like to hyperlink to a Microsoft Word document. At present each record contains a document name and a full file path to where the document is stored. I would like to open Word and view the specified document.

Answer » The details below show how to Open the Microsoft Word application and view the document specified in the associated record on the form.

Paste the following code into a module:

'------------------Code Start-----------------------
Sub OpenWordDoc(strDocName As String)
    Dim objApp As Object

    'Opens the document

    Set objApp = CreateObject("Word.Application")
    objApp.Visible = True
    objApp.Documents.Open strDocName
End Sub
'------------------Code End-------------------------

Then from the On Click event of the command button on the form (named cmdOpenWordDoc in this example) paste the following (where Me.FilePath is the name of the control on the form storing the path to the associated file):

'------------------Code Start-----------------------
Private Sub cmdOpenWordDoc_Click()
'Check to see that there is a document file path associated with the record
    If IsNull(Me.FilePath) Or Me.FilePath = "" Then
        MsgBox "Please Ensure There Is A File Path Associated " & _
        "For This Document", _
               vbInformation, "Action Cancelled"
        Exit Sub
    Else
        'Check that the document specified in the file path is actually there
        If (Dir(Me.FilePath) = "") Then
            MsgBox "Document Does Not Exist In The Specified Location", _
                   vbExclamation, "Action Cancelled"
            Exit Sub
        Else
            'Opens document in Word
            Call OpenWordDoc(Me.FilePath)
        End If
    End If
End Sub
'------------------Code End-----------------------

This will initially check to see that the Microsoft Access record contains a File Path in the control labelled FilePath. If there is no entry in this field a message box is displayed to inform the user and the action is cancelled.

Error when there is no file path associated with the record
Error when there is no file path associated with the record

If there is a file path entered, the next section If (Dir(Me.FilePath) = "") checks to see that the document is present in the specified location. If there is no document present the action is again cancelled and a message box informs the user.

Error when the document does not exist in the specifed location
Error when the document does not exist in the specifed location

You can now open up Microsoft Word documents stored in any location as long as you specify the full path to the file.

You can also download an example of this Microsoft Access 2000 database from either the Microsoft Access Forms page or the Microsoft Access Downloads page.