Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Macro to be available in documents based on a template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mgirving - 19 Oct 2007 12:40 GMT
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.
Helmut Weber - 19 Oct 2007 13:03 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

mgirving - 19 Oct 2007 13:21 GMT
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
Helmut Weber - 19 Oct 2007 14:59 GMT
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.

:-(

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Jonathan West - 19 Oct 2007 15:10 GMT
> Hi,
>
[quoted text clipped - 8 lines]
>
> 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.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Helmut Weber - 19 Oct 2007 23:53 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Russ - 20 Oct 2007 08:29 GMT
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

> Hi Jonathan,
>
[quoted text clipped - 18 lines]
> having removed userform1,
> or on a new blank document.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Helmut Weber - 20 Oct 2007 11:32 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Russ - 21 Oct 2007 10:13 GMT
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.

> Hi Russ,
>
[quoted text clipped - 16 lines]
> or make a copy of the doc with the macro,
> and open that copy.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.