Run Macro On Exit Is Not Copied In Copied Document

G

George Lee

I am creating a copy of an existing document with the following code:

Dim targetDocument As Document
Set targetDocument = myDocument.Application.Documents.Add
("d:\samples\MyDocument.doc")

The file opens fine but on some form fields, the Run Macro On Exit becomes
blank, even through the macro being run is in that document. How do I prevent
the Run Macro from being stripped?
 
J

Jean-Guy Marcil

George Lee was telling us:
George Lee nous racontait que :
I am creating a copy of an existing document with the following code:

Dim targetDocument As Document
Set targetDocument = myDocument.Application.Documents.Add
("d:\samples\MyDocument.doc")

The file opens fine but on some form fields, the Run Macro On Exit
becomes blank, even through the macro being run is in that document.
How do I prevent the Run Macro from being stripped?

I see two problems, one is probably an oversight on your part when you
posted:

1) Set targetDocument = myDocument.Application
what is "myDocument" in that line?

2) Documents.Add expects a template as a parameter, not a document. You are
using a document, so I would not be surprised that things do not go as you
want... The link between the form fields and their macros are broken when
when Word is forced to create a document from a document as if it was trying
to create a new document from a template.

Why are you doing this? What is your overall goal?

--

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

George Lee

The first code line example is an oversight. However, the second was
intended. Using a dot file keeps the macro links. Thanks. However, in
general, I use that line to create a copy of a document (as a .doc), rather
than opening the original document. Is that way not perferred?
 
J

Jean-Guy Marcil

George Lee was telling us:
George Lee nous racontait que :
The first code line example is an oversight. However, the second was
intended. Using a dot file keeps the macro links. Thanks. However, in
general, I use that line to create a copy of a document (as a .doc),
rather than opening the original document. Is that way not perferred?

Documents.Add is not intended to be used to make copy of existing documents,
but to create documents from templates,or create templates from templates.
So if you use it to some other end, you may get unexpected behaviour...

--

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