Discussion Locked

Handle an Error in Access with Word

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Thanks to another member I got this far. I am getting the file name of a document from a database, stringing it together and opening it in Word. "Works great" But If the document does not exist I get an Error. What I am trying to do is if the file does not exist I want to display a message box telling the user to notify me. Now I intercept the error so the program does not stop, but Word opens still opens(with a blank screen. I want only the message box My code follows
Public Sub OpenWordDoc(asFileName As String)
Dim WordApp As Object

'-- Create instance of Word
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True

'--open the file
On Error GoTo MissingGram_Err
WordApp.Documents.Open FileName:="\\mee_pdc\Hyster gram\grams\" & asFileName & ".doc"

Set WordApp = Nothing

MissingGram_Err:
'If IsNull(FileName) Then
'MsgBox "file Not found, inform the Administrator"

'End If

End Sub

Dennis

3 replies

by it_cowboy

Before you assign the Word Object, use the DIR command in Access to check the files existence, if the file does not exist, it will return an empty string ...

Dim strFileName as String
strFileName = "\\mee_pdc\Hyster gram\grams\" & asFileName & ".doc"

If Dir(strFileName) = "" Then
MsgBox "File " & strFileName & " does not exist"
Exit Sub
End If

This code will check if the file exists, if it does not, it will exit the routine, otherwise it will continue on and open the file that it knows exists.

This discussion is closed to new replies.