Can I use a macro to copy Document_New Sub from 1 template to anot

T

toolmaker

Is there a way to use a macro to copy the Document_New event from 1
template's ThisDocument object to another's?

I would like to add the following, PARTICULARLY the CustomizationContext, to
the new Template's ThisDocument object:

---------------------------------------
Private Sub Document_New()
On Error GoTo bye
CustomizationContext = ActiveDocument.AttachedTemplate

If Application.Version >= "11.0" Then
CommandBars("Task Pane").Visible = False
End If
bye:
End Sub
----------------------------------------

PS: I do know how to copy a module and a toolbar. After setting variables
to specific templates, I do it like this:

---------------------------------------
Application.OrganizerCopy Source:=nodTemplate, _
Destination:=stTemplate, _
Name:="NODmacros", Object:=wdOrganizerObjectProjectItems
Application.OrganizerCopy Source:=nodTemplate, _
Destination:=stTemplate, _
Name:="NOD_Mailer_Macros", Object:=wdOrganizerObjectCommandBars
 
W

Word Heretic

G'day toolmaker <[email protected]>,

Yes and no.

No, you cannot copy the module directly since it HAS to exist in the
target.

Yes, you can copy all the LINES from your module to the target module.


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


toolmaker reckoned:
 
T

toolmaker

Thanks for your feedback Word Heretic. Actually, there is a way!

I found a clue to it here:
<http://www.vba-programmer.com/Word_Code/Adding_Code_Programatically.txt>

The trick involves the "AddFromString" function of
ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule .

Here's my solution:

Sub AddThisDocEvent()
'''''''''''''''''''''''''''''''''''''''''''''''''
' Author: Noel C. Gordon (toolmaker)
' Revised: May 19, 2005
''''''''''''''''''''''''''''''''''''''''''''''''''

' This macro opens a template from the user's default
' template directory and adds a 'ThisDocument' event.
' Place this macro in any document EXCEPT your target templates.

Dim TemplatePath As String
Dim myDoc As Document
Dim myDocName As String
Dim fd As FileDialog
Dim strN As String
Dim StandingCodeLines As Long

' Find the Template Directory.

TemplatePath = Options.DefaultFilePath(wdUserTemplatesPath)

' Select the Template to be modified.

MsgBox ("In the next dialog, select the template you wish to modify.")
ChangeFileOpenDirectory TemplatePath
With Dialogs(wdDialogFileOpen)
If .Show = -1 Then
myDocName = .SelectedItems.Item(1)
Else
MsgBox ("You've hit 'Close' or 'Cancel'. " _
& "The macro will now stop.")
Exit Sub
End If
End With
Set myDoc = ActiveDocument

' Handle the Template like a Document

Documents.Open FileName:=myDocName, _
AddToRecentFiles:=False
Set myDoc = ActiveDocument
myDoc.Activate

' Delete any existing code in the "ThisDocument" event

StandingCodeLines =
ActiveDocument.VBProject.VBComponents("ThisDocument") .CodeModule.CountOfLines
If StandingCodeLines > 0 Then
ActiveDocument.VBProject.VBComponents("ThisDocument") _
.CodeModule.DeleteLines 1, StandingCodeLines
End If

' Example of a "ThisDocument" Event: Private Sub Document_New()
' Be VERY CAREFUL in constructing this text string. ...
' Try it out in a ThisDocument module first.

' Insert your code in the area with Large Red type

strN = "Option Explicit" & vbCrLf _
& "Private Sub Document_New()" & vbCrLf & vbTab _
& "On Error Resume Next" & vbCrLf & vbTab _
& " ' Insert your code here." & vbCrLf & vbTab _
& vbCrLf & "End Sub" & vbCrLf


ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString strN

myDoc.Close Savechanges:=wdSaveChanges
Set fd = Nothing
MsgBox ("The task is completed.")

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top