Solved

Copy procedure in ThisDocument to another document

Posted on 2014-09-15
11
131 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 
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 46

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Added a column screws up code 5 57
Problem to With line 4 57
Regarding Notepad++ 4 43
Microsoft Word startup Issue 7 25
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

778 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