Word 2003 "File Not Found"

VentureFree

Well-known member
Joined
Jan 9, 2008
Messages
54
Programming Experience
5-10
I'm writing a program in VB.net that imports some data into a Word 2003 document to be formatted and printed using a template document. I have a Try/Catch block around the .Documents.Open statement to handle when the file is not found, but upon testing it doesn't work. Here's what I'm doing:
VB.NET:
        Try
            ' Open the file.
            docTemplate.Documents.Open( _
                FileName:=CType(strPath & strTemplateFile, Object), _
                ReadOnly:=True, _
                AddToRecentFiles:=False, _
                Format:=Word.WdOpenFormat.wdOpenFormatAuto)
        Catch ex As System.IO.FileNotFoundException
            MsgBox("Could not find template file: " & strTemplateFile & ".")
            Exit Sub
        End Try

The above opens the file fine as long as the file exists at the place where it's supposed to. If it doesn't exist there, the program simply crashes rather than running the code inside the Catch block. How do I get it to run that code? (Note: I actually do more than just a MsgBox, but for display purposes that's all I'm including here).
 
Doh! Me am dum. I needed to catch System.Runtime.InteropServices.COMException. I just had to look at the error that was occurring instead of assuming I already knew it.
 
Back
Top