I've always wondered how to make one of those neat little plug-ins for Visual Basic 6.0 that insert code into the file you're working on. I'm no master, but I finally started checking about and have it proof-of-concepted, at least.

In all of my vb apps, I like to use a separate module to handle errors, in case I want to start changing the way they're handled. By having it pulled out, I've got one place that I have to alter to make error handling change globally. But scrawling that error handling crap the same way every time I'd add a method drove me crazy.

Enter the "AddIn" project in VB6. When you start a new project, select "AddIn". Add a button called cmdInsertText (for this example). Insert this code:

'ย Insertย textย intoย codeย module.
Privateย Subย cmdInsertText_Click()
ย ย ย ย Dimย vbCompย Asย VBComponent
ย ย ย ย Dimย strToInsertย Asย String
ย ย ย ย 
ย ย ย ย Setย vbCompย =ย VBInstance.ActiveVBProject.VBE.SelectedVBComponent
ย ย ย ย 
ย ย ย ย strToInsertย =ย vbNewLineย &ย _
ย ย ย ย ย ย ย ย "publicย subย "ย &ย Me.Text1.Textย &ย vbNewLineย &ย _
ย ย ย ย ย ย ย ย "onย errorย gotoย errHand"ย &ย vbNewLineย &ย _
ย ย ย ย ย ย ย ย vbNewLineย &ย _
ย ย ย ย ย ย ย ย vbNewLineย &ย _
ย ย ย ย ย ย ย ย "exitย sub"ย &ย vbNewLineย &ย _
ย ย ย ย ย ย ย ย "errHand:"ย &ย vbNewLineย &ย _
ย ย ย ย ย ย ย ย vbTabย &ย _
ย ย ย ย ย ย ย ย "Callย mdlErr.errp("""ย &ย vbComp.Nameย &ย "."ย &ย Me.Text1.Textย &ย """,ย err)"ย &ย _
ย ย ย ย ย ย ย ย vbNewLineย &ย _
ย ย ย ย ย ย ย ย "endย sub"

ย ย ย ย vbComp.CodeModule.AddFromStringย strToInsert
Endย Sub


... and then build it. Voila. It's in the Add-Ins menu for all your other VB projects, and it'll insert a new sub at the top of your current file. Woohoo!

Needs some extra stuff to be really useful, but a decent start. Got most of what I needed from the VB6 help topic "Manipulating Code with Add-Ins" and F2.