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
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