Search code examples
vbams-accessms-wordms-access-2016

Opening Word Doc From DblClick event on Listbox


I'm trying to use a double click event on my list box SOPList to open the selected Word document.

It runs the first time you double click a document in the list but the next time you double click the document it gives:

"The remote server machine does not exist or is unavailable".

The error is on line Documents.Open FileToOpen.

The location is a network drive. It is visible and connected.

Private Sub SOPList_DblClick(Cancel As Integer)
    Dim FilePath As String
    Dim FileName As String
    Dim FileToOpen As String
    Dim objWord As Word.Application

    Set objWord = CreateObject("Word.Application")
      
    If SOPList.ListIndex > -1 Then
        FilePath = "\\page\data\NFInventory\groups\CID\SOPs\"
        FileName = Me.SOPList.Value
        FileToOpen = FilePath & FileName
    
        Documents.Open FileToOpen '<---error line
    End If
        
    objWord.Visible = True
    Set objWord = Nothing
    
End Sub

Solution

  • Try changing

    Documents.Open FileToOpen
    

    to

    objWord.Documents.Open FileToOpen