1
votes

I have a VBA addin which I want to run every time someone opens a document. It's working fine for opening existing documents (AutoOpen) and creating new documents from the File > New menu (AutoNew) but when I just open Word up for the first time, neither of these events are firing. The only event I can seem to hook into is the AutoExec event and this is not great as the document doesn't exist yet so ActiveWindow is null.

Can anyone help?

Sub AutoNew
    MsgBox "New"
End Sub

Sub AutoOpen
    MsgBox "Open"
End Sub

Sub AutoExec
    MsgBox "Exec"
End Sub
1
See here - your question doesn't say when/how you are trying those events but it seems they cannot be stored in an Addin.enderland
You can do this using event handlers. See here for an example in Excel: msdn.microsoft.com/en-us/library/office/…. Beware, though; Word is not great at triggering events consistently. If you double-click a file rather than opening it from Word, for instance, certain Application events don't fire. For my purposes, this is ok (the actions I want to trigger are not essential) but if you need a guarantee that the events will fire this may not help you. Here's a list of the application events: msdn.microsoft.com/en-us/library/office/…Christina
Thanks for the comments. So can you help me with which event will fire when I launch Word that will allow me to modify a property on ActiveWindow?Mark

1 Answers

1
votes

I would start with DocumentOpen and NewDocument. An additional level of complexity exists if you need to support ProtectedView documents; Word triggers a different event. I've found that if I try to check for that event (and it doesn't occur) it raises an error. I didn't had much luck and eventually it wasn't worth the time I was spending. I've posted an example below of some code that opens the Style Pane when a document is opened or a new one created (assuming the add-in is loading) and expands the style margin in draft view if not already expanded.

In my UI module:

Dim X As New clsAppEvent 'This is in the declarations

Public Sub OnRibbonLoad(objRibbon As IRibbonUI)
    Do While Documents.Count = 0
      DoEvents
    Loop ' I find this useful as sometimes it seems my ribbon loads before the document. 
    Call Register_Event_Handler
    ' Other stuff
End Sub

Private Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub

Then, in a class module I call clsAppEvent:

Option Explicit

Public WithEvents App As Word.Application

Private Sub App_DocumentOpen(ByVal Doc As Document)
    App.TaskPanes(wdTaskPaneFormatting).visible = True
End Sub

Private Sub App_NewDocument(ByVal Doc As Document)
    App.TaskPanes(wdTaskPaneFormatting).visible = True
End Sub

Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
    If Wn.StyleAreaWidth <= 0 Then
      Wn.StyleAreaWidth = 60
    End If
End Sub

Other than the caveats I've noted above, the one problem I've had is if a user has Auto code in their Normal template as well. This has only come up once so I haven't investigated it.

I wish I could find the site where I learned about this (and from which the Register_Event_Handler was derived. If I find it I'll add a comment.