Which document is this "Document_New" Event running for RRS feed

  • Question

  • I am using Office16.

    When a new document is created from a template the "Document_New" Event runs on the template but that means "ThisDocument" refers to the template not the document that has just been created from the template. Normally the new document will be "Document1" but this is not always the case so how can I always access the document that caused the "Document_New" event?

    Thanks for any help

    Thursday, March 23, 2017 3:27 AM


  • To ensure you know which document you'll be addressing when you're working with multiple documents, or with template-based code, you could use something along the lines of:

    Dim wdDoc1 as Document, wdDoc2 as Document
    Set wdDoc1 = ActiveDocument
    Set wdDoc2 = Documents.New(Template:="MyTemplate")

    The above code allows that you might have one document that's already open and that you want to create a new one and provide for some data interchange between the two. For example:

    wdDoc2.Range.FormattedText = wdDoc1.Sections(1).Range.FormattedText

    would replicate the content of Section 1 of the original document in the new document.

    As for the Document_New event, any ThisDocument Property refers to the template; the ActiveDocument Property refers to the document.

    Paul Edstein
    [MS MVP - Word]

    • Edited by macropodMVP Thursday, March 23, 2017 4:32 AM
    • Marked as answer by FSComp Thursday, March 23, 2017 9:40 AM
    Thursday, March 23, 2017 4:27 AM