hj_daisy
asked on
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.
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.
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
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.
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
@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.
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.
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:=ActiveDocumen t.Name, Name:="<Name of the module>", Object:=wdOrganizerObjectP rojectItem s
If you put all your procedures in the same module, then copying them around is easy.
Application.OrganizerCopy Source:= "<Path>\<OriginalTemplate>
Destination:=ActiveDocumen
If you put all your procedures in the same module, then copying them around is easy.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
ASKER
CopyProcedure AppResps_GotFocus
CopyProcedure MonthYear_GotFocus
CopyProcedure TitleName_GotFocus
etc.
Thanks...