Solved

Word Macro's in templates

Posted on 2006-11-15
3
522 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

726 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