Reputation: 11
Hi i am getting runtime error 1004 not inserted object when i am uploading document in Ms Excel using OleObject. The detail of my code is given below. I will be thankful for guidance in this regard.
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
Upvotes: 1
Views: 316
Reputation: 43585
Lets guess... Is it happening on this line:
With ThisWorkbook.Worksheets("L2G").Range("D13").Select
If this is the case, delete the line and write the following:
ThisWorkbook.Activate
ThisWorkbook.Worksheets("L2G").Activate
ThisWorkbook.Worksheets("L2G").Range("D13").Select
Upvotes: 0
Reputation: 25286
I don't think user Vityata's solution is completely correct. It should be:
With ThisWorkbook.Worksheets("L2G")
'Insertion of the object
Set hussain = .OLEObjects.Add(Filename:=eric, Link:=False, displayasicon:=True)
End With
It selects the worksheet of the current workbook (if there are more workbooks open, you might want to replace the ThisWorkbook
with Workbooks("myName")
). It uses only the .
in the Set
statement because the correct workbook and sheet was already selected with the With
statement.
Upvotes: 1