Macro in Dot but not in Doc

Q

Question Boy

Is there a way to have vba in a template that gets run when a new document is
create, but does not get saved as part of the document itself?

Thank you,

QB
 
T

Tony Jollans

That is the normal way it works anyway. When a new document is created from
a template, VBA code remains in the template but is not copied to the
document.
 
Q

Question Boy

Mine is not behaving in that manner.

I launch the dot and It runs my VBA, generates the doc. I save it and when
I reopen the .doc it executes the VBA again (it has imbedded the VBA from the
..dot into the .doc).

Any advice is appreciated.

QB
 
G

Gordon Bentley-Mix

Tony is correct. However, if the user has access to the template from which
the document is created - for example, it's in the Workgroup Templates
folder or rolled out to the users' machines via a login script or some such
mechanism - the user will still have access to any code in the template.
This process allows code to be written so that the user can perform
document-specific operations post-document creation; e.g. rerun the
template, work with some specific feature in the document, etc.

You can set up your code so that the last operation after the document is
built is to disconnect it from the original template by using something like
ActiveDocument.AttachedTemplate = Normal.dot. There are some risks
associated with this process, but nothing that can't be worked around
easily.

Of course, if the users _don't_ have access to the template - for example,
if you email it to someone outside of the organisation - then it becomes a
non-issue.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
Q

Question Boy

When I check in the VBE there is not code in the .doc, but it does have a
refence to the .dot and I cannot disable it.

QB
 
G

Gordon Bentley-Mix

What does your code do? Post it - or at least the bit that's running again -
and we'll see if we can spot the problem.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
G

Gordon Bentley-Mix

This is standard behaviour. You cannot remove the reference to the attached
template through the VBE. You must change it using native Word
functionality; e.g. (in Word 2003) Tools | Templates and Add-ins and change
the value under 'Document template'. Or you could set the attached template
via code as discussed in my previous post.

However, if you post your code as requested, I'm sure there's a way around
the problem of the code running again.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
T

Tony Jollans

When you re-open the document, assuming the template is available, code in
the template can still run, but code run when a new document is created
(Document_New event, or AutoNew code) will not run when an existing document
is opened.

As Gordon says, you can attach a different template (one without code) to
the document when your code has run, or before saving.
 
Q

Question Boy

Gordon,

Below is the code that run, calling other functions & subs...

Private Sub Document_New()
On Error GoTo Error_Handler

Const vbYes = 6

response = MsgBox("Would you like to import the database data at this
time?", vbYesNo)
If response = vbYes Then
Call GenHeader
Call GenDataTbl
End If

If Err.Number = 0 Then Exit Sub

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: Document_New" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Sub
End Sub

Further testing proved what you stated to be correct. It was not running
the sub again but only prompting to enable macros. As such, I am very
interested in switching the reference back to normal.dot. Where would I need
to place such code to accomplish this?

I can't thank you both for your guidance on this!

QB
 
Q

Question Boy

You guys rock!

i did some digging based on what you posted and figured out that I could use
the following line to switch the reference .dot by placing it right after
executing my 2 routines.

ActiveDocument.AttachedTemplate =
Application.Options.DefaultFilePath(wdUserTemplatesPath) & "\Normal.dot"

I cannot thank you both for your time and help!

I truly learnt something about Word VBA today.

QB
 
G

Gordon Bentley-Mix

Glad to be of assistance.

BTW, the location of the Normal template is "known" to Word, so you
shouldn't have to specify the full path to it; it should be sufficient to
simply use:

ActiveDocument.AttachedTemplate = "normal.dot"

At least this appears to work OK for me in the immediate window in the VBE.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
T

Tony Jollans

Actually, Word knows the name of the Normal Template and, as it changes in
2007, it would be far better to use:

ActiveDocument.AttachedTemplate = NormalTemplate

Well done for working it out!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top