file not found

N

new nerd

I need the following statements to result in a message box
when the user types in a file that does not exist. Does
anyone know why it doesn't work? THANKS!!

Dim pName As String

pName = InputBox("Enter Name of Workbook File or Click
OK to Exit")
If Len(pName) = 0 Then
MsgBox ("File not found")

End If
Selection.InsertFile FileName:="c:/Workbook/" & pName
 
J

Jezebel

All this does at the moment is display the message if the user doesn't enter
a filename. There are several methods you could use here:

1. Validate the filename using the Dir() function.

2. Try to insert the file anyway, and trap the error:

on error resume next
Selection.InsertFile FileName:="c:/Workbook/" & pName
if err.number <> 0 then
msgbox "Failed to insert file" 'File doesn't exist, or is not a
file than can be inserted
end if

3. Use a common dialog control to select the file. This is by far the best
way, since the user is forced to select an existing file, and you can also
restrict the type of file selected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top