?
Solved

Copy procedure in ThisDocument to another document

Posted on 2014-09-15
11
Medium Priority
?
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 48

Expert Comment

by:Dale Fye
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Expert Comment

by:Dale Fye
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1000 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 48

Assisted Solution

by:Dale Fye
Dale Fye earned 1000 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 49

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

764 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