2016-08-02 10 views
0

私はフォームを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 
+0

何かをする前に選択した項目数を確認してください。 –

答えて

0

これは画像ユーザーフォームおよび挿入のために働く必要があり、主な変化は、我々はウィンドウピッカー用Office.FileDialogDimと画像と設定を選択し、代わりに、我々は、単にパスを使用して画像オブジェクトを用いて分離することです。 _Clickの名前を調整する必要があります。

Public PicPath As String 'this is where we'll store the picture path 

Private Sub Browse_Click() 'getting the picture path 

Dim fDialog As Office.FileDialog 
Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 

With fDialog 

    .AllowMultiSelect = False 
    .ButtonName = "Submit" 
    .Title = "Select an image file" 
    .Filters.Add "Image", "*.gif;*.jpg;*.jpeg", 1 

    If .Show = -1 Then 
     PicPath = .SelectedItems(1) 
     'getting picture full path to use when submit click 
    End If 

    If PicPath = "" Then Unload Me 

End With 

End Sub 

Private Sub Submit_Click()'setting picture in active cell 

    If Not PicPath = "" Then 
     CellAddress = ActiveCell.Address 
     With ActiveSheet.Pictures.Insert(PicPath) 
      With .ShapeRange 
       .LockAspectRatio = msoTrue 
       .Height = 150 
      End With 
      .Left = ActiveSheet.Range(CellAddress).Left 
      .Top = ActiveSheet.Range(CellAddress).Top 
      .Placement = 1 
      .PrintObject = True 
     End With 
     picselectedbool = True 
    Else 
     msgbox ("Please select a picture") 
    End If 

    If picselectedbool Then 
     Unload Me 
    End If 

End Sub 
+0

ありがとう!ただし、送信ボタンをクリックするとコードが自動的に表示されるようになりました。エラー文を使用して解決できますか? –

+0

こんにちは、あなたの目標は、Excelシートに画像を挿入することだと思っていました。私はそれを誤解したと思うので、あなたは画像挿入が(写真を開いて挿入する間に起こると思われるもののように)あなたが期待するものを正確に教えてください。 @白羽ルカ –

+0

私が期待したのは、ユーザーフォームを使って画像を選択でき、ユーザーフォーム上の送信ボタンを押すと、自動的に画像が目的のセルに挿入されるということでした。混乱させて申し訳ありません。 @Miguel_Ryu –