Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Jan 2002
    Location
    Bismarck, North Dakota, USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Chaining Templates (Word 2002)

    I have created two separate templates that each work fine when run alone. I am trying to add a command at the end of the first template to automatically run the second template. The actual command works fine - my template opens - but the AutoNew commands are applied to the first template window instead of the second.

    How do I set my second template as the active document so that the code executes in that window.

    Any suggestions would be greatly appreciated.
    Missy

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Chaining Templates (Word 2002)

    So... if you step through with the de###### does it work correctly? What is the first line of code where it outputs to the wrong document? Does it involve ActiveDocument or Selection?

    Assuming you are "opening" the second template using:

    Documents.Add Template:=mySecondTemplate, etc.

    I wonder whether there is a parameter to force focus on the new document? If not, then it might be necessary to figure out which of the various documents in the Documents collection is the newest one. You could do this by checking for a blank path and then the creation time:

    Documents(index).Path = vbNullString
    Documents(index).BuiltInDocumentProperties(wdPropertyTimeCreated)

    If your new documents are created in the same minute, the results might be indeterminate. In that case, you might have to parse the integer that appears after the word Document in the new name...which sounds like even more fun. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Wish I could help further, but I don't fully understand what's happening with your templates.

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Chaining Templates (Word 2002)

    You may be able to get this to work, but it would probably require moving the code you currently have in the AutoNew for the second template, into the first template (or alternatively, possibly having the code in both templates).

    The determining factor in how you deploy the code would be: do you need the flexibility of sometimes creating documents based on the second template alone, or will the second template only be used to create documents in tandem with the first template?

    If the latter, then you can move the code from the AutoNew for the second template in to the first one, along the lines of:

    <pre>Dim FirstDoc As Document
    Dim SecondDoc As Document

    'set reference to first doc in case needed later:
    Set FirstDoc = ActiveDocument
    Set SecondDoc = Documents.Add("YourTemplatePath&Name")
    SecondDoc.Activate 'this line not really necessary
    'do stuff with SecondDoc
    FirstDoc.Activate 'leave user back in first document
    'the above really only needed if you have used "SecondDoc.Activate"

    Set FirstDoc = Nothing
    Set SecondDoc = Nothing
    </pre>

    With this approach, you are doing stuff to the new document based on the second template, but the code is actually located in the first template - this is the only way (that I know of) to ensure that the code runs in the second document.

    If you need the flexibility of sometimes creating new documents directly from the second template, then you do need to have some code in AutoNew (or Document_New) in the second template. The tricky part then is working out how to have the necessary code in both the first and the second template, without having them conflict if both run.

    Gary

  4. #4
    New Lounger
    Join Date
    Jan 2002
    Location
    Bismarck, North Dakota, USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Chaining Templates (Word 2002)

    I guess I should have provided a little more detail. I need to keep the templates separate. I work in a law office. The first template is a notice and the second is an affidavit of service. The affidavit of service is used with many different court headings, etc. All of my global macros & forms are in my WLF template.

    I have been trying to step through the templates to determine exactly where it switches focus back to the notice template. Here is what I have:

    My first template contains:
    (Dim statements)
    Selection.GoTo What:=wdGoToBookmark, Name:="CourtHeading"
    Application.Run MacroName:="BRCourtHeading" 'This is a global template that gets the information for the court heading and inserts it into the document
    Selection.GoTo What:=wdGoToBookmark, Name:="DocName"
    Selection.TypeText Text:="Notice of Continued

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Chaining Templates (Word 2002)

    <hr>When I choose BR (the same form is called that was called from the Notice template) it brings up the form (the Private Sub UserForm_Initialize() just sets some default button and text box values) but now the Notice template is in the background. The text I entered when I filled in this form when it was called from the Notice template is still there. Good so that I don't have to retype the case information but could that be causing my problem?<hr>
    If I interpret this correctly, when you call the UserForm from the first template, the associated document is somehow being made the active window. I'm not sure is this is "how it works" or whether there might be something in your code for that form that activates the document. Can you see the sequence of events here? Liberal use of Stop statements can be helpful in these cases.

  6. #6
    New Lounger
    Join Date
    Jan 2002
    Location
    Bismarck, North Dakota, USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Chaining Templates (Word 2002)

    I have not used the Stop statement before. I will give that a try.

    Thanks for the suggestion.

  7. #7
    New Lounger
    Join Date
    Jan 2002
    Location
    Bismarck, North Dakota, USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Chaining Templates (Word 2002)

    I put a stop statement as the last entry of the Private Sub btnOK_Click() in the BRCourtHeading userform but that did not change anything.

    Thanks for your assistance. I will have to take a look at it again and see if I can figure out what would be causing it to switch active screens. Maybe I need to look at changing my basic set up.

    Thank you for all of your help.
    Missy

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Chaining Templates (Word 2002)

    I guess I didn't give the complete explanation. When the macro stops and pops up the editor, you can size the editor to cover, say, the lower half of the screen and use the button on the Debug toolbar to step through the code watching how the two documents behave in the background.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •