VBA code from template is linked to doc file. I need a copy.

J

Joerg Eisentraeger

Hi all,

I've just beginning using VBA in MS Word (2000) and I'm wondering that
VBA code placed in a template is not copied to the file being created on
the base of the template (like in MS Excel), but in the doc file is a
link to the dot file only.

What about if the dot is moved, changed or deleted? My tests have shown
that my doc looses the macro functionality which was in the template.

How can I force that the whole VBA code from the template is _copied_ to
the doc file?


Greatings from Germany
Joerg
 
J

Jezebel

Normally the whole point is that you *don't* want the code attached to the
document, because you want to share the code with all documents based on the
template. (I've always disliked the Excel approach for the same reason.)
Your templates are no more likely to get lost than your documents -- less
so, in fact.

However, if the macros apply only to the one document, it makes sense to put
the code in the document. To do this: open VBA, display the project
explorer, click and drag the code module frome the template to the document.
You can also Export the code module, which means saving it as a text file
that you can later import into another document or template.
 
J

Joerg Eisentraeger

Hello Jezebel,
Normally the whole point is that you *don't* want the code attached to the
document ... Your templates are no more likely to get lost than your documents -- less
so, in fact.

OK, this is true if you are sitting in front of your own stand-alone PC
or in a small network. But my templates are used by few hundreds people
in a large company network. After creating - the doc files are
distributed by email, are checked in into PDM databases etc. The doc
files cannot access the template from their new locations. But the VBA
code has to work.
... to put
the code in the document. To do this: open VBA, display the project
explorer, click and drag the code ...

I can't be sure that each user is able to do this manually in the
correct way. Therefore I need a solution to do this by VBA after
doubleclicking the template.


Greatings from Germany
Joerg
 
J

Jezebel

Joerg Eisentraeger said:
Hello Jezebel,


OK, this is true if you are sitting in front of your own stand-alone PC
or in a small network. But my templates are used by few hundreds people
in a large company network. After creating - the doc files are
distributed by email, are checked in into PDM databases etc. The doc
files cannot access the template from their new locations. But the VBA
code has to work.


Distributing documents containing macros via email will fail on most
corporate networks for security reasons; and even if not, the user will get
security warnings when they open the document. Distributing code is always
challenging. The recommended approach with Word is to set up the users to
share a workgroups templates folder located on the server and save the
template to that location.

I can't be sure that each user is able to do this manually in the
correct way. Therefore I need a solution to do this by VBA after
doubleclicking the template.

Of course they won't. If you want to put the code into the document itself,
YOU do it before distributing the document. How can the user double-click
the template if you're not going to distribute the template?
 
J

Joerg Eisentraeger

Hi Jezebel,
The recommended approach with Word is to set up the users to
share a workgroups templates folder located on the server and save the
template to that location.
I know and understand this, but in my special cases this approach is not
the appropriate one. (And for Excel it is not used too.)
Of course they won't. If you want to put the code into the document itself,
YOU do it before distributing the document.
The doc file is distributed by the user creating it, not by _me_.
How can the user double-click
the template if you're not going to distribute the template?
The template _is_ as you said on a templates folder located on the
server. Each user has read access to it. If a new doc is to be created
the _user_ doubleclicks on the template.

After distributing the doc file it cannot find its template. That's the
problem. With Excel I don't have this problem.


Greatings from Germany
Joerg
 
J

Jezebel

Well if you can find a way to explain your problem, perhaps someone can
suggest a solution...it certainly isn't obvious from your posting so far.
 
J

Joerg Eisentraeger

Well if you can find a way to explain your problem, perhaps someone can
suggest a solution...it certainly isn't obvious from your posting so far.

OK. If you create a doc using a template dot (not the normal.dot) the
new doc gets a reference to the "attached template". In the doc
properties the attached template is specified by its UNC name
\\server1\folder\template.dot . And: All the VBA code of the template
remains in the template only.

If the attached template, its folder, or its share is missing or has
been moved or renamed - i.g. the IT department has changed the server
name from server1 to server2 - then the doc file cannot find its
attached template and the VBA code therein. The code cannot be executed.

Therefore I am looking for a VBA solution inside the template that
_copies_ the VBA code from the template to the new doc when the new doc
is created using the template.


BTW: The MS Word approach that a doc allways needs its attached template
causes other problems too, see
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q823372


Greatings from Germany
Joerg
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Joerg Eisentraeger > écrivait :
In this message, < Joerg Eisentraeger > wrote:

||| Well if you can find a way to explain your problem, perhaps someone can
||| suggest a solution...it certainly isn't obvious from your posting so
far.
||
|| OK. If you create a doc using a template dot (not the normal.dot) the
|| new doc gets a reference to the "attached template". In the doc
|| properties the attached template is specified by its UNC name
|| \\server1\folder\template.dot . And: All the VBA code of the template
|| remains in the template only.
||
|| If the attached template, its folder, or its share is missing or has
|| been moved or renamed - i.g. the IT department has changed the server
|| name from server1 to server2 - then the doc file cannot find its
|| attached template and the VBA code therein. The code cannot be executed.
||
|| Therefore I am looking for a VBA solution inside the template that
|| _copies_ the VBA code from the template to the new doc when the new doc
|| is created using the template.
||
||
|| BTW: The MS Word approach that a doc allways needs its attached template
|| causes other problems too, see
|| http://support.microsoft.com/default.aspx?scid=kb;EN-US;q823372
||

Play around with the following code to get what you need. See the online
help for more info on each object/property. You would need code in the
Document_New procedure (ThisDocument module).

'_______________________________________
Dim i As Long

With ActiveDocument.VBProject
For i = 1 To .VBComponents.Count
MsgBox .VBComponents(i).Name
Next i
End With

'ActiveDocument.VBProject.VBComponents("Name").Export
'ActiveDocument.VBProject.VBComponents.Import "MyFile.bas"
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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