Solved

Word VBA Document_Open or AutoOpen on template

Posted on 2011-09-13
3
2,488 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
ID: 36529713
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
ID: 36529784
The template is in the global template and addins.
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 36530051
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

823 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