?
Solved

Word VBA Document_Open or AutoOpen on template

Posted on 2011-09-13
3
Medium Priority
?
2,719 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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