Cancel loadpicture

mike0123m

Board Regular
Joined
Jul 25, 2007
Messages
93
I have the following code on a userform to preview an image before inserting onto a worksheet. The problem is that if I push the cancel button instead of selecting a picture to insert on the userform, I get an error that the picture cannot be loaded and then a run-time error 53. What is the trick to tell Excel that cancel means no picture was selected so the image form on the userform and on the worksheet remain empty?

Private Sub CommandButton9_Click()
Dim vntfile As String
Dim strFilters As String
Dim r As Range

strFilters = "All Image files,*.bmp;*.gif;*.jpg;*.jpeg,Bitmap (*.bmp),*.bmp"
vntfile = Application.GetOpenFilename(strFilters)
If vntfile <> "" Then
TextBox21.Text = vntfile
LoadImage TextBox21.Text

End If

Sheets("sheet4").Image1.Picture = LoadPicture(vntfile)

end sub



I thought I had the canel feature covered in the following:

Sub LoadImage(Name As String)

If Dir(Name) <> "" Then
With Image3
.AutoSize = True
.Picture = LoadPicture(Name)
m_sngHeight = .Height
m_sngWidth = .Width
.AutoSize = False
.PictureSizeMode = fmPictureSizeModeStretch
m_sngZoom = 1
End With
SetZoom m_sngZoom
Else
MsgBox "Unable to load image " & Chr(10) & Name, vbExclamation
End If

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try

Rich (BB code):
Dim vntfile As Variant
Dim strFilters As String
Dim r As Range

strFilters = "All Image files,*.bmp;*.gif;*.jpg;*.jpeg,Bitmap (*.bmp),*.bmp"
vntfile = Application.GetOpenFilename(strFilters)
If vntfile <> False Then
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top