PDA

View Full Version : Solved: OLEObject Click Event



alita
03-12-2009, 07:08 AM
Hi all!
I stumbled upon just another issue where google right now couldn't help me:

In an excel file I generate an ActiveX CommandButton dynamically via program code. Now I want to program a Click-procedure for this single button. How do I have to do it? For example, the name of the button is "CmdOk"
(Please forgive me as this question may be just a noobie's one...)

Bob Phillips
03-12-2009, 07:17 AM
Dim StartLine As Long

With ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
StartLine = .CreateEventProc("Click", "CommandButton1") + 1
.InsertLines StartLine, _
"Dim ans" & vbCrLf & _
" ans = Msgbox( ""All OK"",vbYesNo)" & vbCrLf & _
" If ans = vbNo Then Cancel = True"
End With

alita
03-12-2009, 08:20 AM
Thanks a lot. That is exactly what I was looking for! And please excuse me, if those questions were bugging or annoying someone!