'Apply code to active document not document opened with Documents.Add

I got a VBA project from colleague who is retired.

I have to change a function:

Documents.Add Template:= _
    "c:\word\Link.dot", _
    NewTemplate:=False, DocumentType:=0

Here a new document (another Word file) is created with a template.

This template also relates data from another project "Common".

Basically, Documents.Add Template:= _"c:\word\Link.dot" in Link.dot the Document_Open() is executed and the Common project is initialized.

Private Sub Document_Open()
    Common.Initialize
End Sub

I don't want a second document opened by Documents.add, it should use the already active document.

I tried these two variants:

#1

Dim oDoc As Document
Set oDoc = ActiveDocument
oDoc.AttachedTemplate = "c:\word\Link.dot"

#2

ActiveDocument.AttachedTemplate = "c:\word\Link.dot"

Nothing happens to both of them, even no Runtime Error. I think it's because Document_Open() wasn't executed.



Solution 1:[1]

Document_Open() responds to the Open event. As you are not opening a document when attaching the template it will not execute.

You can execute the code directly though.

ActiveDocument.AttachedTemplate = "c:\word\Link.dot"
Common.Initialize

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Timothy Rylatt