Global templates and AutoNew

User avatar
Cecilia
StarLounger
Posts: 89
Joined: 19 Feb 2010, 16:56
Location: San Francisco, Alameda, CA

Global templates and AutoNew

Post by Cecilia »

Hi All!

I am working on a global template that references external templates. The global template (loaded from the Word startup folder) contains a routine that fills in fields (if they exist) in a document that's created--and right now I have that as a button on a toolbar on the global template and it works when pressed. But instead of requiring the user to click the button to fill in the fields, I want it to happen automagically every time a new document is started from a template. To do that, I created an AutoNew sub, but it doesn't seem to fire off (unless the global template is opened as a regular template, which I don't care about because the global template doesn't contain any fields, only code to open the other templates).

Is it impossible to make the AutoNew happen when the template is loaded as a global template? Is there some magic button that I need to push to get AutoNew to happen from the global template every time a new document is opened? If not, is there some other way I could accomplish this (without having to edit each of the individual templates)?

Thanks!

Cecilia :)

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Global templates and AutoNew

Post by HansV »

One option is to store the AutoNew macro in each user's default template (Normal.dot or Normal.dotm, depending on the version of Word), but this is probably not an attractive option.

Another option is to create an application-level event handler:

- Create a class module and name it clsEvent.
- In this class module, declare a variable

Public WithEvents app As Application

- Create an event handler for the NewDocument event:

Code: Select all

Private Sub app_NewDocument(ByVal Doc As Document)
    ' Your code goes here
    ...
End Sub
- In the ThisDocument module of the template, declare a variable

Dim MyClass As New clsEvent

And initialize the variable in the Document_Open event:

Code: Select all

Private Sub Document_Open()
  Set MyClass.app = Application
End Sub
Best wishes,
Hans

User avatar
Cecilia
StarLounger
Posts: 89
Joined: 19 Feb 2010, 16:56
Location: San Francisco, Alameda, CA

Re: Global templates and AutoNew

Post by Cecilia »

HansV wrote:One option is to store the AutoNew macro in each user's default template (Normal.dot or Normal.dotm, depending on the version of Word), but this is probably not an attractive option.
The (main) problem with this is that if you don't happen to have the global template loaded in your system, having the autonew macro generates a compile error on the sub in the global template (because it doesn't exist), and I can't figure out how to capture and handle something like that.

Trying your second option, although it's making my head spin, as it appears difficult and beyond my knowledge. Either it didn't work, or I didn't do it right, and I'm not sure which it is. I can't even get a message box to fire on document open. :(

Thanks! :)

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Global templates and AutoNew

Post by HansV »

I have attached a zip file with two templates (one in Word 97-2003 format and one in Word 2007 format). If you save the one for your version in Word's startup folder, then start Word, you should hear a beep whenever you create a new document.
Global.zip
As you'll see, there is not much code.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Cecilia
StarLounger
Posts: 89
Joined: 19 Feb 2010, 16:56
Location: San Francisco, Alameda, CA

Re: Global templates and AutoNew

Post by Cecilia »

HansV wrote:I have attached a zip file with two templates (one in Word 97-2003 format and one in Word 2007 format). If you save the one for your version in Word's startup folder, then start Word, you should hear a beep whenever you create a new document.
Global.zip
As you'll see, there is not much code.
The Office 2003 sample worked PERFECTLY! :thankyou:

Now I'm off to look for other things I can implement with your magic code!

Thanks, Hans
:clapping:

User avatar
Cecilia
StarLounger
Posts: 89
Joined: 19 Feb 2010, 16:56
Location: San Francisco, Alameda, CA

Re: Global templates and AutoNew

Post by Cecilia »

I spoke too soon. It worked beautifully to launch the code on new template. But none of the code in my toolbar will run anymore. "The macro cannot be found or has been disabled due to your security settings." Even if I set my security settings to the lowest possible, it didn't help.

:sad:

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Global templates and AutoNew

Post by HansV »

I guess the first part of the error message "The macro cannot be found" is the relevant part here. Where do the macros called by the toolbar buttons reside?
Best wishes,
Hans

User avatar
Cecilia
StarLounger
Posts: 89
Joined: 19 Feb 2010, 16:56
Location: San Francisco, Alameda, CA

Re: Global templates and AutoNew

Post by Cecilia »

HansV wrote:I guess the first part of the error message "The macro cannot be found" is the relevant part here. Where do the macros called by the toolbar buttons reside?
I have one template, called "Toolbar", that is loaded from the Startup folder globally.

The template contains a toolbar that performs various functions. Each function is either a sub located in basMain in the toolbar, or a form with code behind it. The toolbar buttons do things like lookup addresses from tables in Access databases or retreive user information from the registry. The only button on the toolbar that doesn't function this way is a button that opens a link to a Sharepoint site where all of the individual templates are located in a library.

So I'm guessing the fact that the code is in the toolbar module(s) means it's not available to the class that was created when the document was created?

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Global templates and AutoNew

Post by HansV »

And what is the name of the template containing the code based on my example?
Best wishes,
Hans

User avatar
Cecilia
StarLounger
Posts: 89
Joined: 19 Feb 2010, 16:56
Location: San Francisco, Alameda, CA

Re: Global templates and AutoNew

Post by Cecilia »

HansV wrote:And what is the name of the template containing the code based on my example?
It was all in one template.

I separated your example out, and it works fine with two separate templates. I was just hoping to keep it to one file so I wouldn't have to set the path to the database in more than one place.

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Global templates and AutoNew

Post by HansV »

Sorry, I can't explain that. It works fine for me with the toolbar and all the code in one global template.
Best wishes,
Hans

User avatar
Cecilia
StarLounger
Posts: 89
Joined: 19 Feb 2010, 16:56
Location: San Francisco, Alameda, CA

Re: Global templates and AutoNew

Post by Cecilia »

HansV wrote:Sorry, I can't explain that. It works fine for me with the toolbar and all the code in one global template.
Puzzling. But at least I'm 150% further on than I was before I asked for help! :)

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Global templates and AutoNew

Post by HansV »

I have attached a zip file with my test global template.
Global2.zip
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Cecilia
StarLounger
Posts: 89
Joined: 19 Feb 2010, 16:56
Location: San Francisco, Alameda, CA

Re: Global templates and AutoNew

Post by Cecilia »

Thanks for the example again. I need to learn to write the toolbar like that. Always something new from you, Hans! :)

I think it was a public sub that I had in there that might've caused a problem. Ie. I was calling a function and a sub from the main module into the class module. When I separated the code out, the problem went away. It was a good exercise, at any rate, the code is probably 10 years old and needed some refreshing ;)

:thankyou: