Global templates and AutoNew
-
- StarLounger
- Posts: 89
- Joined: 19 Feb 2010, 16:56
- Location: San Francisco, Alameda, CA
Global templates and AutoNew
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 :)
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 :)
-
- Administrator
- Posts: 78625
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Global templates and AutoNew
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:
- In the ThisDocument module of the template, declare a variable
Dim MyClass As New clsEvent
And initialize the variable in the Document_Open event:
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
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
Hans
-
- StarLounger
- Posts: 89
- Joined: 19 Feb 2010, 16:56
- Location: San Francisco, Alameda, CA
Re: Global templates and AutoNew
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.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.
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! :)
-
- Administrator
- Posts: 78625
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Global templates and AutoNew
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.
As you'll see, there is not much code.
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
Hans
-
- StarLounger
- Posts: 89
- Joined: 19 Feb 2010, 16:56
- Location: San Francisco, Alameda, CA
Re: Global templates and AutoNew
The Office 2003 sample worked PERFECTLY!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.
As you'll see, there is not much code.
Now I'm off to look for other things I can implement with your magic code!
Thanks, Hans
-
- StarLounger
- Posts: 89
- Joined: 19 Feb 2010, 16:56
- Location: San Francisco, Alameda, CA
Re: Global templates and AutoNew
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.
-
- Administrator
- Posts: 78625
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Global templates and AutoNew
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
Hans
-
- StarLounger
- Posts: 89
- Joined: 19 Feb 2010, 16:56
- Location: San Francisco, Alameda, CA
Re: Global templates and AutoNew
I have one template, called "Toolbar", that is loaded from the Startup folder globally.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?
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?
-
- Administrator
- Posts: 78625
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Global templates and AutoNew
And what is the name of the template containing the code based on my example?
Best wishes,
Hans
Hans
-
- StarLounger
- Posts: 89
- Joined: 19 Feb 2010, 16:56
- Location: San Francisco, Alameda, CA
Re: Global templates and AutoNew
It was all in one template.HansV wrote:And what is the name of the template containing the code based on my example?
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.
-
- Administrator
- Posts: 78625
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Global templates and AutoNew
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
Hans
-
- StarLounger
- Posts: 89
- Joined: 19 Feb 2010, 16:56
- Location: San Francisco, Alameda, CA
Re: Global templates and AutoNew
Puzzling. But at least I'm 150% further on than I was before I asked for help! :)HansV wrote:Sorry, I can't explain that. It works fine for me with the toolbar and all the code in one global template.
-
- Administrator
- Posts: 78625
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Global templates and AutoNew
I have attached a zip file with my test global template.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- StarLounger
- Posts: 89
- Joined: 19 Feb 2010, 16:56
- Location: San Francisco, Alameda, CA
Re: Global templates and AutoNew
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 ;)
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 ;)