Insert Photo - error on cancel

M

mooresk257

Hi,

I have the following code for a picture box to load an image file:

Private Sub Image1_Click()

FileToOpen = Application.GetOpenFilename("All Files
(*.jpg),*.jpg,(*.bmp),*.bmp")
Worksheets("Sheet1").OLEObjects("Image1").Object.Picture _
= LoadPicture(FileToOpen)

End Sub

which works fine unless I click "cancel" instead of selecting an image file,
at which point there is an error.

How to I allow the process to abort cleanly on "cancel"?

Thanks!
 
M

Martin Fishlock

Hi:

You need to check the cancel button on return from getting the file name. Try:

Private Sub Image1_Click()

FileToOpen = Application.GetOpenFilename( _
"All Files (*.jpg),*.jpg,(*.bmp),*.bmp")
If FileToOpen <> False Then
Worksheets("Sheet1").OLEObjects("Image1").Object.Picture _
= LoadPicture(FileToOpen)
End If

End Sub
 

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