Solved

Word VBA Document_Open or AutoOpen on template

Posted on 2011-09-13
3
2,442 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Text wrapping issue with Word 14 40
*Best* PDF to Word Convertor 4 111
How to strip word docx from 'template-fields' after mailmerge? 3 85
Word 2016 Search Comments 5 32
There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
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…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

911 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

19 Experts available now in Live!

Get 1:1 Help Now