[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2998
  • Last Modified:

Word VBA Document_Open or AutoOpen on template

Hi Experts

I have a template that is in the set up to load in the word-addins folder, I want to use the docment_open (or autoopen) to get the active document name when ever a word document is opened

I have the code
Public Sub Document_Open()
MsgBox ActiveDocument.Name
End Sub

or Module AutoOpen.
Public Sub Main()
MsgBox ActiveDocument.Name
End Sub

Both events fire when I open the document template within the add-in folder but dont run when I open any other document, any ideas?
0
MrDavidThorn
Asked:
MrDavidThorn
1 Solution
 
GrahamSkanRetiredCommented:
Which template is the code in? If you want to to fire for every document, it must be in a global template. The Normal template is global as are any in the Word Startup folder.
0
 
MrDavidThornAuthor Commented:
The template is in the global template and addins.
0
 
andrewssd3Commented:
If it is a global template, you can add this code to the ThisDocument module of the global template:

Option Explicit

Public WithEvents wdApp As Word.Application

' set the event handler to the word application when this doc is opened to all global event handling
Private Sub Document_Open()
    Set wdApp = Application
End Sub

Private Sub wdApp_DocumentOpen(ByVal Doc As Document)
    MsgBox Doc.Name
End Sub

Open in new window


This is basic - if you want more code to handle application-level events it's normal to define a class module to do it.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now