Running A Macro From Another Document

G

George Lee

I’d like to run a macro from another document but I keep getting a can’t run
macro error. The help file says the following

Application.Run "'My Document.doc'!ThisModule.ThisProcedure"

If you specify the document name, your code can only run macros in
documents
related to the current context — not just any macro in any document.

What does that mean? What is the ‘current context’?
 
M

Manfred F

Hi George,

George Lee said:
I’d like to run a macro from another document but I keep getting a can’t run
macro error. The help file says the following

Application.Run "'My Document.doc'!ThisModule.ThisProcedure"

the correct syntax should be
Application.Run "ProjectName.ModuleName.ProcedureName"
where "ProjectName.ModuleName.ProcedureName" would be unique for the word
application object.

But due to a bug, this won't work (W2003). It has to be
Application.Run "UniqueProcedureName"
where UniqueProcedureName is unique for the word application object.

Kind Regards
Manfred
 
G

George Lee

Thanks but I still don't understand. If the macro is in the external file
"'MyDocument.doc" how do you specify the its location? The format
Application.Run "UniqueProcedureName"
seems to run macros within the same document. How do you access external
documents?
 
S

Steve Yandl

George,

Try something like

Application.Documents.Add("C:\Test\MyDocument.doc")
Application.Run "UniqueProcedureName"


Steve Yandl
 
D

David Horowitz

You can only call the macro if the file it's in is either loaded or
Referenced from your calling project.
Then you can use the exclamation point.
That's why Steve said to Open it first:
Application.Documents.Add("C:\Test\MyDocument.doc")
But after it's open, you can refer to it in several ways:
Application.Run "ProcName"
Application.Run "ModuleName.ProcName"
Application.Run "ProjectName.ModuleName.ProcName"
Application.Run "Filename!ModuleName.ProcName"
 
G

Gordon Bentley-Mix

And to make things even simpler, I would probably put the macro into a
template and save it in the Startup folder so it then becomes an add-in and
you would no longer need to worry about opening the document first because
it would be loaded automatically when you start Word - but then that's just
me. ;-P
--
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!
 

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