Macro to be available in documents based on a template

M

mgirving

I would like a macro I have created in a template (.dot) to be available in
documents (.doc) created from the template (ie. when other people in my
workplace double-click on the template. At the moment, when I double-click
on the template (it creates a .doc version of it )and I go to the list of
macros, the macro I created doesn't show up in the .doc version. What am I
doing wrong. Thank you for any assistance you can offer.
 
H

Helmut Weber

Hi,

macros in templates are not transferred
or copied to the doc created from the template.

If you want to have all the macros from a project
of the template in the doc, you'd use something like:


Application.OrganizerCopy Source:="C:\Edit2003\Normal.dot", _
Destination:= "C:\Test\Word\fox-01.doc", _
Name:="UserForm1", _
Object:= wdOrganizerObjectProjectItems

If it is only about one single macro,
things get complicated.

Explore:
Microsoft Visual Basic for Applications Extensibility 5.3

You version may vary from mine.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
M

mgirving

Thank you Helmut.

I am interested in the suggestion you kindly offered (below). Where do I
insert that text? Thank you again.

If you want to have all the macros from a project
of the template in the doc, you'd use something like:
Application.OrganizerCopy Source:="C:\Edit2003\Normal.dot", _
Destination:= "C:\Test\Word\fox-01.doc", _
Name:="UserForm1", _
Object:= wdOrganizerObjectProjectItems
 
H

Helmut Weber

Hi,

there is something wrong with destination.
Can't get it to work.

I get "Command failed" whatever I do.

I'm so sorry.

Start a new thread like "OrganizerCopy destination".

Maybe somebody else knows better than me.

:-(

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jonathan West

Helmut Weber said:
Hi,

there is something wrong with destination.
Can't get it to work.

I get "Command failed" whatever I do.

I'm so sorry.

Start a new thread like "OrganizerCopy destination".

Maybe somebody else knows better than me.

If I recall, a failure will occur if either of the following conditions is
true

1. An item of that name already exists in the destination

2. Either the source or destination contains a VBA project that has been
protected against viewing.

There may be other reasons as well, but those are the two I remember off the
top of my head.
 
H

Helmut Weber

Hi Jonathan,

very weird, indeed.

I can record a macro without problems.

Sub Macro6()
Application.OrganizerCopy Source:="C:\Edit2003\Normal.dot",
Destination:= _
"C:\Test\Word\organizer.doc", Name:="UserForm1", Object:= _
wdOrganizerObjectProjectItems
End Sub

The userform was copied.
I remove userform1 from organizer.doc.
I can record the same macro again without problems.

Yet, when trying to run the macro,
I get "Command failed".
Doesn't matter whether I try it on the same document,
having removed userform1,
or on a new blank document.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Russ

Helmut and Jonathan,
Apparently Microsoft broke the code for automation for copying between
certain sources and destinations, to avoid spreading macro viruses. They
allow you to manually do what the recorded macro recorded. But then not
allow it to run under automation.

+++++++Quote
This method has one notable limitation: you cannot copy project items from
the Normal template to any other file. This is by design, to prevent the
spread of macro viruses.

In addition, you cannot copy projects to or from a template that is loaded
as an add-in or reference, unless the template is open in Word for editing.
++++++++UnQuote

<http://support.microsoft.com/kb/211466/EN-US/>
+++++++Quote
Application.OrganizerCopy
Source Target Valid Err Error Message
------------------------------------------------------------------------
Document Document Yes
Document Template Yes
Document Normal Yes
Document Global Temp No* 5940 The project item cannot be copied
Template Document Yes
Template Template Yes
Template Normal Yes
Template Global Temp No* 5940 The project item cannot be copied
Normal Document No 4198 Command failed
Normal Template No 4198 Command failed
Normal Normal No 4198 Command failed
Normal Global Temp No 4198 Command failed
Global Temp Document No 4198 Command failed
Global Temp Template No 4198 Command failed
Global Temp Normal No 4198 Command failed
Global Temp Global Temp No 5940 The project item cannot be copied

NOTE: You can copy from a document or template to a global template if the
global template is open in Word for editing.
++++++++UnQuote
 
H

Helmut Weber

Hi Russ,

thx for the information.

Though not even organizercopy
from one document to another is working.

Application.OrganizerCopy _
Source:="C:\Test\Word\MyNormal.doc", _
Destination:="C:\Test\Word\Import.doc", _
Name:="UserForm1", _
Object:=wdOrganizerObjectProjectItems

No way.

If it has to be, my advice to the OP would be,
to put the macro in a doc, open that doc
and save it under another name,
or make a copy of the doc with the macro,
and open that copy.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Russ

Another workaround as suggested in the link I gave with the previous
information:
=======Quote
To work around this behavior, use the VBComponent Export method to save your
module or form as a BAS or FRM file, and then use the VBComponent.Import
method to import the file into the project that you want.

For example, use the following sample code:
Sub ExportImportModule()
'Export Module1 from Normal to a BAS file
VBE.VBProjects("Normal").VBComponents("Module1").Export _
FileName:="C:\My Documents\Module1.bas"
'Import this BAS file into the desired file
VBE.VBProjects("DocProj").VBComponents.Import _
FileName:="C:\My Documents\Module1.bas"
End Sub
=========UnQuote
I think this code requires a reference to VBA extensions or extensibility
enabled in the VBA Editor menu Tools/References... Dialog.
 

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