PDA

View Full Version : [SOLVED:] How to run code on loading global template?



AndrewMc
10-29-2013, 02:17 AM
Hi All

I am trying to modify an Excel add-in to work for Word 2007. While I am comfortable working with Excel VBA, I am new to using VBA with Word.
The Excel add-in is known as SourceTools.xla (hosted at CodeProject, sorry I can't yet post a link) and provides a toolbar in the VB Editor to enable working with a Subversion source repository.

While I am able to modify the code and change things like use of "ThisWorkbook" to "ThisDocument", and can run the code from within the VBE, I cannot seem to get the toolbar to appear. In Excel, simply opening the .xla file or loading the add-in causes Auto_Open() to run and make the new toolbar appear in the VBE. Similarly Auto_Close() is run to remove the toolbar when the .xla file is closed or the add-in unloaded.


Sub Auto_Open()
''' Create the VBE menu.
On Error Resume Next
Set gclsMenuHandler = New CMenuHandler
On Error GoTo 0
End Sub

Sub Auto_Close()
Set gclsMenuHandler = Nothing
End Sub

I have created a SourceTools.dot template file and have tried both opening the file in Word and loading the file as an add-in (via Word Options). In both cases, the Auto_Open code does not appear to run. I thought that perhaps Auto_Exec() is needed (see the code below) but that doesn't seem to work either.


Sub Auto_Exec()
''' Create the VBE menu.
MsgBox "Starting SourceTools Auto_Exec()..."
On Error Resume Next
Set gclsMenuHandler = New CMenuHandler
On Error GoTo 0
End Sub

Sub Auto_Exit()
Set gclsMenuHandler = Nothing
MsgBox "Exiting SourceTools, Auto_Exit()..."
End Sub

Does anyone have any suggestions for things I can try to get the toolbar generating code (or any code for that matter) to run when the template file is loaded?

Cheers
Andrew Mc

fumei
10-29-2013, 03:14 PM
Global templates loaded as add-ins are loaded, not opened. So AutoOpen does not fire. You say you open the template file, but are you really? If you say double click it, then you are not opening it, you are USING it - creating a new document. Again, AutoOpen will not fire. However, you double click the template (using it) and you have an AutoNEW...then that will fire.

AndrewMc
10-29-2013, 09:08 PM
Thank you for the explanation about the difference between loading and add-in and opening a template.

I solved the problem of getting code to run when an add-in is loaded or unloaded by using AutoExec and AutoExit.
In the global template I created a module named AutoExec and in that module made a single procedure called Main. This Main procedure contained the code to be run when the global template is loaded.

In AutoExec module:

Sub Main()
''' Template load code.
MsgBox "Template is loading"
End Sub


To have code run when the template is unloaded, in the global template I created another module named AutoExit and in that module made a single procedure called Main.
In AutoExit module:

Sub Main()
''' Template unload code.
MsgBox "Template is now unloading"
End Sub

Cheers
Andrew Mc