Results 1 to 8 of 8
  1. #1
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Attached template (All)

    Hi

    I take a document produced using Template 1, make changes, attach Template 2, then would like to do some more, e.g generate TOC, save document, display message etc. But what I find is that, as soon as I attach Template 2 to the document - rest of the process never gets called (i.e. generate TOC, save document, display message). The template is still loaded only not attached to the current document.

    Any ideas how to fix this? I would like to save the document with the new template attached. Example code in Template 1 is as follows:

    Open document using Template 1
    ...
    ...
    ' Change FC & BC (covers)
    ChangeFCandBC

    ' Install & attach new template [/b]Template 2[/b]
    AddIns.Add FileName:=ActiveDocument.AttachedTemplate, Install:=True
    AttachNewDocumentTemplate

    Doesn't do any of this as template 2 is attached above
    MsgBox ("New Document template attached")

    UpdateAllFields ' update all fields
    UpdateTableOfContents ' Only update the Page Numbers

    ActiveDocument.SaveAs FileName:=sFullFileName ' Save the updated document

    ' ---------------------------------------------------------------------------
    ' Conversion Complete: Inform the user & get out of here
    conversionCompleteMsg = ("Conversion complete." + Chr(10) + Chr(10) + Chr(13) + _
    "Original document saved as: " + "(Old)" + sFileName + Chr(10) + Chr(10) + Chr(13) + _
    "Converted document saved as: " + sFileName)
    MsgBox (conversionCompleteMsg)
    ClearTheClipboard ' Clear the clipboard of conversion text
    Application.ScreenUpdating = True
    RestoreTC (True)
    End Sub

    Thanks.

    Robie
    Thanks.
    Robie

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Attached template (All)

    The behavior is to be expected: as soon as the document is attached to another template, the macro it was running is not part of the attached template any more, so it ends.
    Can't you place the code that attaches the new template at the very end of the macro?
    If that is not possible, you should put all the code in an add-in instead of in the original template, and run it from there.

  3. #3
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached template (All)

    Yes Thanks Hans.

    Even if I use a add-in how would I save the document after attaching the new template? Are you saying that the add-in would continue to process after attaching the new template? That is the code fragment in my original mail would continue after attaching the template because it is in an add-in?

    Thanks.

    Robie
    Thanks.
    Robie

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Attached template (All)

    You had a document attached to Template 1, and ran a macro stored in Template 1. This macro attached the document to Template 2. The moment you do that, the macro can't continue running since it is stored in Template 1, and the document is no longer attached to that template.
    If you store the macro in Add-in 3 (which should *not* coincide with either Template 1 or Template 2), it does not matter to which template the document is attached. The macro can continue running without problems, since Add-in 3 is independent of both Template 1 and Template 3.

  5. #5
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached template (All)

    Thanks a bunch. More power to you.
    Thanks.
    Robie

  6. #6
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached template (All)

    I have hit a snag. I have created an add-in template number 3.

    The process now is:

    Public Sub MAIN()
    Dim sConvertMsg As String

    If ActiveDocument.Name <> ActiveDocument.AttachedTemplate Then
    sConvertMsg = ("Convert document to use the new document template?")
    Select Case (MsgBox(sConvertMsg, vbYesNo))
    Case vbYes
    ' -----------------------------------------------------------------------------------
    ' Convert current document to 'new document' template by
    ' invoking a conversion process in the add-in 'AutoConvert.dot'
    AddIns.Add FileName:="P:templatesAutoConvert.dot", Install:=True
    AutoConvert ' Call process in the add-in just loaded
    End Select
    End If
    Sub

    Call procedure in the add-in - doesn't work (it can't find the macro - it says 'Sub or Function not defined'). The add-in template is loaded successfully and I can see it under the VBA code.

    Do I need to set it up as a reference?

    Thanks

    Robie.
    Thanks.
    Robie

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Attached template (All)

    Try
    <code>
    Application.Run "NameOfMacro"
    </code>
    with the appropriate name substituted, of course. If the name of the macro is not unique, you can use
    <code>
    Application.Run "NameOfAddin!NameOfModule.NameOfMacro"
    </code>
    again, with the appropriate names substituted.

  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: Attached template (All)

    Well, you can't necessarily rely on the VBE's Project Explorer to know what code is available to a particular document. The document's scope includes itself, its attached template, Normal.dot, other global templates, and COM Add-ins. Often the Sub procedures available to the document will appear in the Macros dialog (Alt+F8) (Subs that take parameters and Functions do not appear there.)

    You might be able to get your add-in into the appropriate context using a reference to it in your attached template. But what will happen when that template is detached? Hmmmm... probably the global template approach is better.

Posting Permissions

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