Solved

Word VBA Document_Open or AutoOpen on template

Posted on 2011-09-13
3
2,412 Views
Last Modified: 2012-05-12
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
Comment
Question by:MrDavidThorn
3 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
 

Author Comment

by:MrDavidThorn
Comment Utility
The template is in the global template and addins.
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now