ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Insert Photo - error on cancel (https://www.excelbanter.com/excel-programming/427923-insert-photo-error-cancel.html)

mooresk257

Insert Photo - error on cancel
 
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.P icture _
= 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!



Martin Fishlock

Insert Photo - error on cancel
 
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.P icture _
= LoadPicture(FileToOpen)
End If

End Sub
--
Hope this helps
Martin Fishlock, www.nyfconsultants.com
Please do not forget to rate this reply.


"mooresk257" wrote:

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.P icture _
= 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!



mooresk257

Insert Photo - error on cancel
 
Thanks - that's much cleaner code than what I had come up with!

"Martin Fishlock" wrote:

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.P icture _
= LoadPicture(FileToOpen)
End If

End Sub
--
Hope this helps
Martin Fishlock, www.nyfconsultants.com
Please do not forget to rate this reply.


"mooresk257" wrote:

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.P icture _
= 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!




All times are GMT +1. The time now is 12:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com