Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2007

Tip: Looking for answers? Try searching our database.

Insert procedure into templates

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jaydee - 26 Apr 2007 05:56 GMT
I need to insert a procedure into an existing module in numerous document
templates. Is there any way to script this? All templates have the module and
the procedure must be inserted into a specific location in the module. I
would also like to modify an existing procedure (eg add additional lines) in
the same module.

Any help would be appreciated.
DA - 27 Apr 2007 05:08 GMT
Hi Jaydee

Programmatically changing VBA code in Word is possible but I advise caution.
Test your code thoroughly step by step. This sort of thing can easily run
amok and cause some serious headaches so make sure you BACK EVERYTHING UP!!!

The following should get you on the right track or at least give you some
ideas to experiment further. You need to set a reference to the Microsoft
Visual Basic  Extensibility library.  

I’ve left out any code dealing with opening up the templates which is
trivial in view of the rest. This code assumes you have a Template_1.dot file
already open and that it contains a routine called "MyTestSub()".

BTW, did I mention .. “BACK EVERYTHING UP!!!” :-)

Good luck, hope this helps.
Dennis

'------------------------------
Sub EditMySub()
Dim aMdl As VBComponent
Dim aPrj As VBProject
Dim lLin As Long
Dim strMyCode   As String
Dim strSub2Edit As String

'Change this string to reflect the
'same name as the procedure you want to edit
strSub2Edit = "MyTestSub"
'strMyCode contains the code you want to insert
strMyCode = "MsgBox ""Hi I'm a piece of Code"""

Set aPrj = Documents("Template_1.dot").VBProject

'This will place your code into "Module1" of your
'project. The For Next loop is simply there to make
'sure the collection member exists.
For Each VBComponent In aPrj.VBComponents
   If VBComponent.Name = "Module1" Then
       Set aMdl = aPrj.VBComponents("Module1")
       With aMdl.CodeModule
         ' Find the first line of the Sub
         iLin = .ProcBodyLine(strSub2Edit, vbext_pk_Proc) + 1
         ' Insert your code
         .InsertLines iLin, strMyCode
       End With
       Exit For
   End If
Next VBComponent
   
'This example will add a whole new module and sub, putting
'the same code in it
Set aMdl = aPrj.VBComponents.Add(vbext_ct_StdModule)
aMdl.CodeModule.InsertLines 1, "Private Sub MyNewShinySub() " _
& vbCrLf & strMyCode & vbCrLf & "End Sub"

'Cleanup and exit
Set aMdl = Nothing
Set aPrj = Nothing

End Sub
'---------------------------------

> I need to insert a procedure into an existing module in numerous document
> templates. Is there any way to script this? All templates have the module and
[quoted text clipped - 3 lines]
>
> Any help would be appreciated.
Jaydee - 30 Apr 2007 06:08 GMT
Thanks DA,

I don't actually want to insert a new module, just insert a couple of lines
of code in a specific location in an existing sub (eg before a line that
starts with "Insert here"), and a new function in the same module.

JD

> Hi Jaydee
>
[quoted text clipped - 67 lines]
> >
> > Any help would be appreciated.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.