-
Jan 29th, 2015, 01:20 AM
#1
Thread Starter
New Member
vba run time error 1004 cannot insert object
Hi All,
I am using excel sheet which use macro code to add command button at run time. It was working earlier and now in production it started giving the error 1004 cannot insert object and command button was not added.
Even now it was working in my machine where in I have MS office 2007 sp2, but customer has MS office 2010 in one machine and MS office 2007 sp3 in another machine, but in both machine it is not working.
Will this service pack version create any problem?
Can you all please help me on the same. In case of any more details please let me know.
Below is the code, which is used to add and command button called "Refresh" at run time.
Public Sub AddButton(myWS As Excel.Worksheet)
Dim OLEObj As OLEObject
Dim Rng As Range
Dim WS As Worksheet
Dim CodeMod As Object
Dim LineNum As Long
Set WS = myWS
Set Rng = Range("I2")
Rng.ClearFormats
'Set OLEObj = WS.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Top:=Rng.Top + (Rng.Top - 400), Left:=Rng.Left, Height:=Rng.Height * 2, Width:=Rng.Width)
Set OLEObj = WS.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Top:=Rng.Top, Left:=Rng.Left, Height:=Rng.Height * 2, Width:=Rng.Width * 2)
OLEObj.Name = "Refresh"
OLEObj.Object.Caption = "Refresh"
Set CodeMod = ReportWorkbook.VBProject.VBComponents(WS.CodeName).CodeModule
LineNum = CodeMod.createeventproc("Click", OLEObj.Name)
CodeMod.insertLines LineNum + 1, "RefreshReport"
' code for change event in sheet
LineNum = CodeMod.createeventproc("Change", "Worksheet") + 1
CodeMod.insertLines LineNum + 1, "on error resume next " & vbNewLine & "" _
& "If Target.Address = " & """$C$11""" & " Then " & vbNewLine & "" _
& " If Target.NumberFormat <> " & """;;;""" & " Then " & vbNewLine & "" _
& " Application.EnableEvents = False: Application.Undo: Application.EnableEvents = True" & vbNewLine & "" _
& " Target.NumberFormat = " & """;;;""" & ": Application.Range(" & """A1""" & ").Activate: Exit Sub" & vbNewLine & "" _
& " EndIf" & vbNewLine & "" _
& " Application.EnableEvents = False:Application.Undo:Application.EnableEvents = True" & vbNewLine & "" _
& " Application.Range(" & """A1""" & ").Activate:Target.Range(" & """C11""" & ").NumberFormat = " & """;;;""" & "" & vbNewLine & "" _
& "EndIf"
'code for Selectchange event in sheet
LineNum = CodeMod.createeventproc("SelectionChange", "Worksheet") + 1
CodeMod.insertLines LineNum + 1, "on error resume next " & vbNewLine & "" _
& "If Target.Address = " & """$C$11""" & " Then Application.CommandBars(" & """Cell""" & ").Enabled = False:Application.CommandBars(" & """Format""" & ").Enabled = False:" _
& "Application.CommandBars(" & """Formatting""" & ").Controls.Item(" & """Cells...""" & ").Enabled = False: Else " _
& "Application.CommandBars(" & """Cell""" & ").Enabled = True:Application.CommandBars(" & """Format""" & ").Enabled = True:" _
& "Application.CommandBars(" & """Formatting""" & ").Controls.Item(" & """Cells...""" & ").Enabled = True:" & vbNewLine & "" _
& "If ActiveSheet.Range(" & """C11""" & ").NumberFormat <> " & """;;;""" & " then ActiveSheet.Range(" & """C11""" & ").NumberFormat = " & """;;;""" & ""
End Sub
Regards,
Vaithilingam.K
-
Jan 29th, 2015, 03:28 PM
#2
Re: vba run time error 1004 cannot insert object
which line gets the error?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 30th, 2015, 01:37 AM
#3
Thread Starter
New Member
Re: vba run time error 1004 cannot insert object
Below is the line throws exception
Set OLEObj = WS.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Top:=Rng.Top, Left:=Rng.Left, Height:=Rng.Height * 2, Width:=Rng.Width * 2)
-
Jan 30th, 2015, 04:45 AM
#4
Re: vba run time error 1004 cannot insert object
I could reproduce the issue too on my PC.
Same error
Following the "Delete MSForms.exd"-Solution on
http://stackoverflow.com/questions/1...osoft-datagrid
i got it working again
Last edited by Zvoni; Jan 30th, 2015 at 04:50 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|