Copy procedure in ThisDocument to another document

I want to copy one or more procedures contained in Template A's ThisDocument location to my active document's ThisDocument location.  Let me see if I can make this more clear.

Template_A contains the following macro in the 'ThisDocument' area:

Sub AppResps_GotFocus()
    With AppResps
       .Clear
       .AddItem "Item 1"
       .AddItem "Item 2"
    End With
End Sub

I want to programmatically copy the procedure called AppResps_GotFocus into my current document, and it needs to go into the ThisDocument area as well.

Template_A will have a project name given to it of 'ProjectA' if that helps with referencing it.  It will also be open in the Editor so there is no need to locate where the template is on the hard drive.

Just wondering if anyone has any code that does that or can shed some light on what I need to use.  

This will be done programatically when the user creates a new template and I do not want to manually copy the code beforehand.  The reason is that the procedures will change a lot over time, so doing this at 'run-time' is the most efficient solution.

I am using Word 2003.  Thank you for your help.
hj_daisyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hj_daisyAuthor Commented:
I should elaborate a bit more.   The document with the macro code will have a lot of other procedures in it and I am hoping to just copy the procedure that I want and not the whole lot.  So I envisage that I would be specifying which procedure name i.e.

CopyProcedure AppResps_GotFocus
CopyProcedure MonthYear_GotFocus
CopyProcedure TitleName_GotFocus
etc.

Thanks...
0
Dale FyeCommented:
I have some code in VBA, that copies code from an Access Module in another mdb/accdb file into the current file.  Not certain whether it will work in VB, but should give you an idea.  Critical to this is that the document must be loaded prior to doing the copy or paste operation.  Hope this helps.

Dim myMod as Module

If app.CurrentProject.AllModules(rs!Document_Name).IsLoaded Then
    bDocWasOpen = True
Else
    app.Application.DoCmd.OpenModule rs!Document_Name
End If
Set myMod = app.Application.Modules(rs!Document_Name)
    
'Copy the function to a string
strCopy = myMod.Lines(rs!Start_Line, (rs!End_Line - rs!Start_Line) + 1) & vbCrLf
    
'Close the document where the function was copied from if it was not already
 'open when entering this procedure
Set myMod = Nothing
If bDocWasOpen = False Then
    app.Application.DoCmd.Close acModule, rs!Document_Name, acSaveNo
End If
       
'Insert copied text into the destination module
'Assumes that you have used code similar to above to set the Destination module
Application.DoCmd.OpenModule DestModule
Set myMod = Application.Modules(DestModule)
myMod.AddFromString strCopy
Application.DoCmd.Save acModule, myMod
Application.DoCmd.Close acModule, myMod.Name

Open in new window

0
GrahamSkanRetiredCommented:
It is possible,  but you are on the edge of the intention of Word's objective, which is to produce dynamically-formatted documents for printing.  Controls and VBA code can be an aid in that, but, in the overall design, once the document is complete, the code is redundant, so does not get transferred to the document.

There are two main ways that it can be done. The simplest is to use the Application.OrganizerCopy method. The other is to use the VBE Project facility. You need to add a reference to the Microsoft Visual Basic for Applications Extensibility' library. You will also need to allow programmatic access  to the VBA code, since this is considered as security vulnerability.

Objects and methods in this library are not very intuitive, but here is some starter code. I'll see if I can get any closer to what you need.

Option Explicit

Sub VBECode()

    Dim VBEProj1 As VBIDE.VBProject
    Dim VBEProj2 As VBIDE.VBProject
    Dim VBEComp1 As VBIDE.VBComponent
    Dim VBEComp2 As VBIDE.VBComponent
    
    Dim mdlCode1 As VBIDE.CodeModule
    Dim mdlCode2 As VBIDE.CodeModule
    
    Set VBEProj1 = Application.Documents("Document1").AttachedTemplate.VBProject
    Set VBEProj2 = Application.Documents("Document1").VBProject
    
    Set VBEComp1 = VBEProj1.VBComponents("ThisDocument")
    
    Set mdlCode1 = VBEComp1.CodeModule
    Debug.Print mdlCode1.Lines(1, 10)
End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Dale FyeCommented:
@hj_Daisy,

After reading Graham's post, and then re-reading your post, I may have misunderstood your intent, as I was not focused on Word.  

Sorry for the confusion this may have caused.
0
Jacques Bourgeois (James Burger)PresidentCommented:
I have never found a way to copy individual pieces of code. But you can copy a whole module with the following command:

  Application.OrganizerCopy Source:= "<Path>\<OriginalTemplate>.dot", _
    Destination:=ActiveDocument.Name, Name:="<Name of the module>", Object:=wdOrganizerObjectProjectItems

If you put all your procedures in the same module, then copying them around is easy.
0
GrahamSkanRetiredCommented:
Here is the completed VBECode macro.
Option Explicit

Sub VBECode()
    Dim iLineCount As Integer
    Dim VBEProj1 As VBIDE.VBProject
    Dim VBEProj2 As VBIDE.VBProject
    Dim VBEComp1 As VBIDE.VBComponent
    Dim VBEComp2 As VBIDE.VBComponent
    
    Dim mdlCode1 As VBIDE.CodeModule
    Dim mdlCode2 As VBIDE.CodeModule
    
    Set VBEProj1 = Application.Documents("Document1").AttachedTemplate.VBProject
    Set VBEProj2 = Application.Documents("Document1").VBProject
    
    Set VBEComp1 = VBEProj1.VBComponents("ThisDocument")
    Set VBEComp2 = VBEProj2.VBComponents("ThisDocument")
   
    Set mdlCode1 = VBEComp1.CodeModule
    Set mdlCode2 = VBEComp2.CodeModule
    
    iLineCount = mdlCode1.CountOfLines
    mdlCode2.AddFromString mdlCode1.Lines(1, iLineCount)
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeCommented:
So, I guess I wasn't off track at all.

In my example, I am copying entire procedures (subs or functions) from one database to another.  I have some code that catalogs those procedures so when I get to this point, I already know the lines of a particular "document" which are the beginning and ending lines.  In my case, I simply copy the original lines into a text string, add the string to the destination module (interestingly, this puts the code at the top of the destination module), then save and close the destination module.
Set myMod = Application.Modules(DestModule)
myMod.AddFromString strCopy
Application.DoCmd.Save acModule, myMod
Application.DoCmd.Close acModule, myMod.Name

Open in new window

0
GrahamSkanRetiredCommented:
Note that the expected way of of doing this is to install the template on to the systems that need to use the macro code.

Events in the ThisDocument module will still fire. However be careful to distinguish between the ActiveDocument and ThisDocument objects. ThisDocument refers to the the document/template that has the code, which would not be the same as the target document.
0
hj_daisyAuthor Commented:
Thank you everyone for your input.  I just wanted to let you know that I haven't abandoned this question and will get back by the weekend as I have a big deadline to finish today and I need time to test the different ideas you have suggested.
0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.