Solved

Word Macro's in templates

Posted on 2006-11-15
3
515 Views
Last Modified: 2010-08-05
Word Macro's in templates

Currently we have code that works perfectly when opening the document up.  The sub procedure "AutoOpen()" placed in the "ThisDocument" module of the Word Objects part of the project runs and will open a userform.  That userform, then uses the ThisDocument command to change the picture of an image control within the document called "imgSpace", using the following code:

ThisDocument.imgSpace.Picture = LoadPicture(ImagePath)

This all works fine....that is until the document is then converted to a template.  When it is a template, it creates a copy of itself (with references to the original template code), the autoopen() procedure will not run and the references to ThisDocument.imgSpace no longer work.

How can i convert the code to automatically run when the template is opened...and only this template (so not global), and also refer to the control "imgSpace" within the new document based on the template.

Hope this all makes sense, and thank you in advance.

Many thanks

Romolo
0
Comment
Question by:roycasella
3 Comments
 
LVL 15

Expert Comment

by:cquinn
ID: 17945748
Go into the VBA editor (Alt+F11)  in the template, then Insert/Module from the menu, right click on the module and rename it to AutoOpen, then insert a proceduer called

Public Sub MAIN()
    'Your code goes here
End Sub
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 17945974
Microsoft is trying to move away from the Auto.. macros. In the case of AutoOpen, you can use the Document_New and Document_Open events in the ThisDocument module of your template.

When you create a new document from the template, ThisDocument refers to the template itself. ActiveDocument will refer to the new document.

 
0
 
LVL 7

Author Comment

by:roycasella
ID: 17946111
cquinn... didnt work im afraid

grahamskan..

yep..worked a treat

many thanks

Romolo
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now