运行时错误1004未插入对象

时间:2016-11-28 12:40:31

标签: vba excel-vba excel

我正在使用OleObject在Ms Excel中上传文档时收到运行时错误1004未插入对象。我的代码的细节如下。我将感谢这方面的指导。

Private Sub UploadL2G_Click()

Dim hussain  As OLEObject
Dim eric As Variant

'Selection of the file
eric = Application.GetOpenFilename

If eric = False Then
    Exit Sub
    CheckL2G.Value = False
    Application.ScreenUpdating = False
End If

'Pasting the object in the wished cell

With ThisWorkbook.Worksheets("L2G").Range("D13").Select
'Insertion of the object
    Set hussain = ActiveSheet.OLEObjects.Add(Filename:=eric, Link:=False, displayasicon:=True)
End With

CheckL2G.Value = True

'Size of the icon
hussain.Width = 30
hussain.Height = 30
Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:1)

我不认为用户Vityata的解决方案是完全正确的。它应该是:

With ThisWorkbook.Worksheets("L2G")
    'Insertion of the object
    Set hussain = .OLEObjects.Add(Filename:=eric, Link:=False, displayasicon:=True)
End With

它选择当前工作簿的工作表(如果有更多工作簿打开,您可能希望将ThisWorkbook替换为Workbooks("myName"))。它仅使用.语句中的Set,因为已使用With语句选择了正确的工作簿和工作表。

答案 1 :(得分:0)

让我猜猜...... 是否发生在这条线上:

With ThisWorkbook.Worksheets("L2G").Range("D13").Select

如果是这种情况,请删除该行并写下以下内容:

ThisWorkbook.Activate

ThisWorkbook.Worksheets("L2G").Activate

ThisWorkbook.Worksheets("L2G").Range("D13").Select