createEventProc("click", "labelFoo")
creates a procedure to catch the click event on the control named labelFoo and returns the line number where the event procedure was inserted. This line number can subsequently be used with codeModule.insertLines
to add the code for the event. dim lineInserted as long lineInserted = application.VBE.ActiveVBProject.VBComponents("form_frmQueryDB").CodeModule.CreateEventProc("click", "labelFoo") application.VBE.ActiveVBProject.VBComponents("form_frmQueryDB").CodeModule.InsertLines lineInserted + 1, " msgBox(""Clicked"")"