PDA

View Full Version : Solved: Moving procedures???



tebule
11-02-2006, 04:46 PM
I need to copy a procedure from a source workbook to another workbook (browsed & chosen by the user). The procedure being copied is a "Workbook_Open" sub that needs to be located in the target workbook's "ThisWorkbook" object. Before copying, I need to identify if the target workbook has a "Workbook_Open" sub already.

Any ideas?:dunno
Cheers,
Tebule

matthewspatrick
11-03-2006, 10:48 AM
Tebule,

What does this have to do with Access?

:)

tebule
11-03-2006, 10:51 AM
I realized later that I should have posted this in the excel forum. :doh:Sorry about that. But I found the answer anyway, so I didn't mess with it.
:beerchug:
Cheers,
tebule

Ken Puls
11-03-2006, 02:44 PM
Moved to Excel forum. :)

tebule, care to share you answer, in case someone else has the same issue?

tebule
11-03-2006, 03:17 PM
Here are the code piece that were used to help. I found it in an article on this site:

Discussion on programming to the VBE window VBE Programming
Published by johnske
09-19-2006


Pieces that were used were:



Sub AddProcedureToNewModule()
Dim LastLinePlusOne As Long
With ThisWorkbook.VBProject.VBComponents("NewModule").CodeModule
LastLinePlusOne = .CountOfLines + 1
.InsertLines LastLinePlusOne, "Sub NewProcedure()" & vbNewLine & _
" Msgbox ""Here is the new procedure"" " & vbNewLine & _
"End Sub"
End With
Application.Run "NewProcedure"
End Sub

Sub CreateAnEventProcedure()
Dim FirstLine As Long
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
FirstLine = .CreateEventProc("Open", "Workbook") + 1
.InsertLines FirstLine, vbNewLine & "Msgbox ""Hello"",vbOkOnly"
End With
End Sub



Cheers,
Tebule:beerchug:

Ken Puls
11-03-2006, 03:22 PM
Cool, thanks! We always like to make sure the solutions are posted, in case someone else has a simliar question.

FYI, I edited the above to make it display correctly. :)