我試圖從用戶窗體插入圖像到excel工作表,但如果當我按窗體上的提交按鈕時沒有選擇圖片它說運行時錯誤'424':所需的對象。我怎麼解決這個問題? 以下是瀏覽按鈕。在vba中插入圖像
Private Sub browse_Click()
Dim pic As Variant
pic = Application.FileDialog(msoFileDialogFilePicker)
With pic
.AllowMultiSelect = False
.ButtonName = "Submit"
.Title = "Select an image file"
.Filters.Add "Image", "*.gif;*.jpg;*.jpeg", 1
If .Show = -1 Then
Me.filepath.Text = .SelectedItems(1)
Me.picpreview.PictureSizeMode = fmPictureSizeModeClip
Me.picpreview.Picture = LoadPicture(.SelectedItems(1))
Else
End If
If pic = False Then Exit Sub
End With
End Sub
而以下是將圖像分配給特定單元的代碼。
Cells(emptyrow, 10).Select
With xlApp.ActiveSheet.Pictures.Insert(picname) 'it is this line the debugger always points to when I submit the form without a picture
With .ShapeRange
.LockAspectRatio = msoTrue
.Height = 150
End With
.Left = xlApp.ActiveSheet.Cells(emptyrow, 10).Left
.Top = xlApp.ActiveSheet.Cells(emptyrow, 10).Top
.Placement = 1
.PrintObject = True
End With
在做任何事之前檢查選定的項數。 –