Solved

Copy procedure in ThisDocument to another document

Posted on 2014-09-15
11
124 Views
Last Modified: 2014-11-03
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.
0
Comment
Question by:hj_daisy
  • 3
  • 3
  • 2
  • +2
11 Comments
 

Author Comment

by:hj_daisy
ID: 40324365
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
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40324998
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40325014
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
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40325104
@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
 
LVL 40
ID: 40325321
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 40327458
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
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 250 total points
ID: 40327517
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40332224
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
 

Author Comment

by:hj_daisy
ID: 40353787
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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40419011
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now