Application.Run calling macro with parameters in other template

C

Chad Knudson

I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.

In a module named modCustom in my Normal.dot template file I've added the
following two subs:

Public Sub TestCallParm(str As String)

MsgBox "This is a test. [" & str & "]"

End Sub

Public Sub TestSubWithParmCall()

Dim strTest As String

strTest = "Hello, World!"

Application.Run MacroName:="modCustom.TestCallParm", varg1:=strTest '
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails

End Sub

Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:

Public Sub TestCall()

MsgBox "This is a test."

End Sub

Public Sub TestCallNoParm()

Application.Run MacroName:="Normal.modCustom.TestCall" ' Succeeds

End Sub
 
P

Peter Hewett

Hi Chad Knudson

Application.Run can be very flakey, especially when using the "Project.Module.Procedure"
variant and parameters. I've found it so problematic that I frequently load the template
containing the macro I want to run as an AddIn and ensure that the procedure is global in
scope (Public). I then restrict usage to Application.Run "Procedure" [, params].

HTH + Cheers - Peter
 
C

Chad Knudson

This one is puzzling me. The example code that was included in Help doesn't
seem to work. I've seen conflicting interpretations of Application.Run,
from one reference that said you cannot pass parameters to a macro, to the
declaration of Application.Run that shows up to 30 parameters to pass to the
specified macro.

The VBA help says that the Application.Run supports passing up to 30
variants as arguments to a macro:
Run Method
See AlsoApplies ToExampleSpecifics
Runs a Visual Basic macro.

expression.Run(MacroName, varg1, varg2, varg3, varg4, varg5, varg6, varg7,
varg8, varg9, varg10, varg11, varg12, varg13, varg14, varg15, varg16,
varg17, varg18, varg19, varg20, varg21, varg22, varg23, varg24, varg25,
varg26, varg27, varg28, varg29, varg30)

expression Required. An expression that returns an Application object.

MacroName Required String. The name of the macro. Can be any combination
of template, module, and macro name. For example, the following statements
are all valid.

Application.Run "Normal.Module1.MAIN"
Application.Run "MyProject.MyModule.MyProcedure"
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.

varg1...varg30 Optional Variant. Macro parameter values. You can pass up
to 30 parameter values to the specified macro.

Remarks
Although Visual Basic code can call a macro directly (without this method
being used), this method is useful when the macro name is stored in a
variable (for more information, see the example for this topic). The
following statements are functionally equivalent.

Normal.Module2.Macro1
Call Normal.Module2.Macro1
Application.Run MacroName:="Normal.Module2.Macro1"Example
This example prompts the user to enter a template name, module name, macro
name, and parameter value, and then it runs that macro.

Dim strTemplate As String
Dim strModule As String
Dim strMacro As String
Dim strParameter As String

strTemplate = InputBox("Enter the template name")
strModule = InputBox("Enter the module name")
strMacro = InputBox("Enter the macro name")
strParameter = InputBox("Enter a parameter value")Application.Run
MacroName:=strTemplate & "." _ & strModule & "." & strMacro, _
varg1:=strParameter"Peter Hewett said:
Hi Chad Knudson

Application.Run can be very flakey, especially when using the "Project.Module.Procedure"
variant and parameters. I've found it so problematic that I frequently load the template
containing the macro I want to run as an AddIn and ensure that the procedure is global in
scope (Public). I then restrict usage to Application.Run "Procedure" [, params].

HTH + Cheers - Peter


I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.

In a module named modCustom in my Normal.dot template file I've added the
following two subs:

Public Sub TestCallParm(str As String)

MsgBox "This is a test. [" & str & "]"

End Sub

Public Sub TestSubWithParmCall()

Dim strTest As String

strTest = "Hello, World!"

Application.Run MacroName:="modCustom.TestCallParm", varg1:=strTest '
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails

End Sub

Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:

Public Sub TestCall()

MsgBox "This is a test."

End Sub

Public Sub TestCallNoParm()

Application.Run MacroName:="Normal.modCustom.TestCall" ' Succeeds

End Sub
 
A

Andrew Savikas

It should puzzle you, because it *should* work. But it doesn't. At least not always, and not the way the documentation says it should. Welcome to the world of Windows ;)

----- Chad Knudson wrote: -----

This one is puzzling me. The example code that was included in Help doesn't
seem to work. I've seen conflicting interpretations of Application.Run,
from one reference that said you cannot pass parameters to a macro, to the
declaration of Application.Run that shows up to 30 parameters to pass to the
specified macro.

The VBA help says that the Application.Run supports passing up to 30
variants as arguments to a macro:
Run Method
See AlsoApplies ToExampleSpecifics
Runs a Visual Basic macro.

expression.Run(MacroName, varg1, varg2, varg3, varg4, varg5, varg6, varg7,
varg8, varg9, varg10, varg11, varg12, varg13, varg14, varg15, varg16,
varg17, varg18, varg19, varg20, varg21, varg22, varg23, varg24, varg25,
varg26, varg27, varg28, varg29, varg30)

expression Required. An expression that returns an Application object.

MacroName Required String. The name of the macro. Can be any combination
of template, module, and macro name. For example, the following statements
are all valid.

Application.Run "Normal.Module1.MAIN"
Application.Run "MyProject.MyModule.MyProcedure"
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.

varg1...varg30 Optional Variant. Macro parameter values. You can pass up
to 30 parameter values to the specified macro.

Remarks
Although Visual Basic code can call a macro directly (without this method
being used), this method is useful when the macro name is stored in a
variable (for more information, see the example for this topic). The
following statements are functionally equivalent.

Normal.Module2.Macro1
Call Normal.Module2.Macro1
Application.Run MacroName:="Normal.Module2.Macro1"Example
This example prompts the user to enter a template name, module name, macro
name, and parameter value, and then it runs that macro.

Dim strTemplate As String
Dim strModule As String
Dim strMacro As String
Dim strParameter As String

strTemplate = InputBox("Enter the template name")
strModule = InputBox("Enter the module name")
strMacro = InputBox("Enter the macro name")
strParameter = InputBox("Enter a parameter value")Application.Run
MacroName:=strTemplate & "." _ & strModule & "." & strMacro, _
varg1:=strParameter"Peter Hewett said:
Hi Chad Knudson
Application.Run can be very flakey, especially when using the
"Project.Module.Procedure"
variant and parameters. I've found it so problematic that I frequently load the template
containing the macro I want to run as an AddIn and ensure that the procedure is global in
scope (Public). I then restrict usage to Application.Run "Procedure" [, params].
HTH + Cheers - Peter
"Chad Knudson" <[email protected]>, said:
I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.
In a module named modCustom in my Normal.dot template file I've added the following two subs:
Public Sub TestCallParm(str As String)
MsgBox "This is a test. [" & str & "]"
End Sub
Public Sub TestSubWithParmCall()
Dim strTest As String
strTest = "Hello, World!"
Application.Run MacroName:="modCustom.TestCallParm", varg1:=strTest
'
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails
End Sub
Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:
Public Sub TestCall()
MsgBox "This is a test."
End Sub
Public Sub TestCallNoParm()
Application.Run MacroName:="Normal.modCustom.TestCall" ' Succeeds
End Sub
 
P

Peter Hewett

Hi Chad Knudson

It just doesn't work properly/consistently! If you want to pass parameters make sure you
use the "Procedure_name" only variant.

Cheers - Peter


This one is puzzling me. The example code that was included in Help doesn't
seem to work. I've seen conflicting interpretations of Application.Run,
from one reference that said you cannot pass parameters to a macro, to the
declaration of Application.Run that shows up to 30 parameters to pass to the
specified macro.

The VBA help says that the Application.Run supports passing up to 30
variants as arguments to a macro:
Run Method
See AlsoApplies ToExampleSpecifics
Runs a Visual Basic macro.

expression.Run(MacroName, varg1, varg2, varg3, varg4, varg5, varg6, varg7,
varg8, varg9, varg10, varg11, varg12, varg13, varg14, varg15, varg16,
varg17, varg18, varg19, varg20, varg21, varg22, varg23, varg24, varg25,
varg26, varg27, varg28, varg29, varg30)

expression Required. An expression that returns an Application object.

MacroName Required String. The name of the macro. Can be any combination
of template, module, and macro name. For example, the following statements
are all valid.

Application.Run "Normal.Module1.MAIN"
Application.Run "MyProject.MyModule.MyProcedure"
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.

varg1...varg30 Optional Variant. Macro parameter values. You can pass up
to 30 parameter values to the specified macro.

Remarks
Although Visual Basic code can call a macro directly (without this method
being used), this method is useful when the macro name is stored in a
variable (for more information, see the example for this topic). The
following statements are functionally equivalent.

Normal.Module2.Macro1
Call Normal.Module2.Macro1
Application.Run MacroName:="Normal.Module2.Macro1"Example
This example prompts the user to enter a template name, module name, macro
name, and parameter value, and then it runs that macro.

Dim strTemplate As String
Dim strModule As String
Dim strMacro As String
Dim strParameter As String

strTemplate = InputBox("Enter the template name")
strModule = InputBox("Enter the module name")
strMacro = InputBox("Enter the macro name")
strParameter = InputBox("Enter a parameter value")Application.Run
MacroName:=strTemplate & "." _ & strModule & "." & strMacro, _
varg1:=strParameter"Peter Hewett said:
Hi Chad Knudson

Application.Run can be very flakey, especially when using the "Project.Module.Procedure"
variant and parameters. I've found it so problematic that I frequently load the template
containing the macro I want to run as an AddIn and ensure that the procedure is global in
scope (Public). I then restrict usage to Application.Run "Procedure" [, params].

HTH + Cheers - Peter


I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.

In a module named modCustom in my Normal.dot template file I've added the
following two subs:

Public Sub TestCallParm(str As String)

MsgBox "This is a test. [" & str & "]"

End Sub

Public Sub TestSubWithParmCall()

Dim strTest As String

strTest = "Hello, World!"

Application.Run MacroName:="modCustom.TestCallParm", varg1:=strTest '
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails

End Sub

Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:

Public Sub TestCall()

MsgBox "This is a test."

End Sub

Public Sub TestCallNoParm()

Application.Run MacroName:="Normal.modCustom.TestCall" ' Succeeds

End Sub

HTH + Cheers - Peter
 
J

Julie

For Peter Hewett,

Peter,

I'm curious... do you mean that you load the project containing the macro as
an addin, AND use application.run to call that project's procedures? IF so,
and if the "referenced" project is loaded as an addin, why not simply add a
VBA reference to the referencING project and call the functions directly, to
both pass parameters and read function return values. I"m very interested
to hear your answer. I do that extensively and it works well for me, but I
concerns me that I don't see the idea discussed here often. If few people
do it that way, then perhaps the functionality may be left behind in future
releases of Word, and that concerns me. My Word knowledge is insignificant
compared to yours, and if you are not using direct referencing, then I am
wondering if I should be. Any thoughts?

Thanks,
Julie

Peter Hewett said:
Hi Chad Knudson

Application.Run can be very flakey, especially when using the "Project.Module.Procedure"
variant and parameters. I've found it so problematic that I frequently load the template
containing the macro I want to run as an AddIn and ensure that the procedure is global in
scope (Public). I then restrict usage to Application.Run "Procedure" [, params].

HTH + Cheers - Peter


I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.

In a module named modCustom in my Normal.dot template file I've added the
following two subs:

Public Sub TestCallParm(str As String)

MsgBox "This is a test. [" & str & "]"

End Sub

Public Sub TestSubWithParmCall()

Dim strTest As String

strTest = "Hello, World!"

Application.Run MacroName:="modCustom.TestCallParm", varg1:=strTest '
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails

End Sub

Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:

Public Sub TestCall()

MsgBox "This is a test."

End Sub

Public Sub TestCallNoParm()

Application.Run MacroName:="Normal.modCustom.TestCall" ' Succeeds

End Sub
 
P

Peter Hewett

Hi Julie

You're dead right that's exactly what I do. It's kludgey and messy but when I need to run
a macro in another template it works more reliably.

The reason I resort to the above is that I want to minimise the number of references made.
Or a range of templates contain the same procedure name but each does something template
specific. So in this case a project reference would not be feasible. Every time you
reference another template from your project Word has to load that template as well. Also
because the references are absolute locations (\\myserver\mytemplates\mytemplate.dot) you
have to update these references if you ever move or rename any of the other templates. Of
course I have a tool for this but it's still a real hassle for my clients! They decide to
reorganise their templates and the code breaks! I prefer transparency and reliability so
use the former technique.

BTW you're far too modest your knowledge of Word is extensive otherwise you would not be
asking these questions!

HTH + Cheers - Peter

For Peter Hewett,

Peter,

I'm curious... do you mean that you load the project containing the macro as
an addin, AND use application.run to call that project's procedures? IF so,
and if the "referenced" project is loaded as an addin, why not simply add a
VBA reference to the referencING project and call the functions directly, to
both pass parameters and read function return values. I"m very interested
to hear your answer. I do that extensively and it works well for me, but I
concerns me that I don't see the idea discussed here often. If few people
do it that way, then perhaps the functionality may be left behind in future
releases of Word, and that concerns me. My Word knowledge is insignificant
compared to yours, and if you are not using direct referencing, then I am
wondering if I should be. Any thoughts?

Thanks,
Julie

Peter Hewett said:
Hi Chad Knudson

Application.Run can be very flakey, especially when using the "Project.Module.Procedure"
variant and parameters. I've found it so problematic that I frequently load the template
containing the macro I want to run as an AddIn and ensure that the procedure is global in
scope (Public). I then restrict usage to Application.Run "Procedure" [, params].

HTH + Cheers - Peter


I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.

In a module named modCustom in my Normal.dot template file I've added the
following two subs:

Public Sub TestCallParm(str As String)

MsgBox "This is a test. [" & str & "]"

End Sub

Public Sub TestSubWithParmCall()

Dim strTest As String

strTest = "Hello, World!"

Application.Run MacroName:="modCustom.TestCallParm", varg1:=strTest '
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails

End Sub

Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:

Public Sub TestCall()

MsgBox "This is a test."

End Sub

Public Sub TestCallNoParm()

Application.Run MacroName:="Normal.modCustom.TestCall" ' Succeeds

End Sub
 
J

Julie

Thanks Peter.

Some insight on this topic from someone else is helpful. Often, I wonder if
I am doing the right thing with the VBA references, but persist anyway
because the value in passing input parameters and reading function return
values is irresistible. That the VBA references are absolute is a big pain
in the neck, I agree, and I can see why if developing for different clients,
you would want to avoid them. I use the feature to reference local drive
global templates only, and that seems to hold up for a single shop where the
organization of templates is standard among users and changes infrequently.
At least it SEEMS to hold up ...!

Thanks,

Julie

Peter Hewett said:
Hi Julie

You're dead right that's exactly what I do. It's kludgey and messy but when I need to run
a macro in another template it works more reliably.

The reason I resort to the above is that I want to minimise the number of references made.
Or a range of templates contain the same procedure name but each does something template
specific. So in this case a project reference would not be feasible. Every time you
reference another template from your project Word has to load that template as well. Also
because the references are absolute locations
(\\myserver\mytemplates\mytemplate.dot) you
have to update these references if you ever move or rename any of the other templates. Of
course I have a tool for this but it's still a real hassle for my clients! They decide to
reorganise their templates and the code breaks! I prefer transparency and reliability so
use the former technique.

BTW you're far too modest your knowledge of Word is extensive otherwise you would not be
asking these questions!

HTH + Cheers - Peter

For Peter Hewett,

Peter,

I'm curious... do you mean that you load the project containing the macro as
an addin, AND use application.run to call that project's procedures? IF so,
and if the "referenced" project is loaded as an addin, why not simply add a
VBA reference to the referencING project and call the functions directly, to
both pass parameters and read function return values. I"m very interested
to hear your answer. I do that extensively and it works well for me, but I
concerns me that I don't see the idea discussed here often. If few people
do it that way, then perhaps the functionality may be left behind in future
releases of Word, and that concerns me. My Word knowledge is insignificant
compared to yours, and if you are not using direct referencing, then I am
wondering if I should be. Any thoughts?

Thanks,
Julie

Peter Hewett said:
Hi Chad Knudson

Application.Run can be very flakey, especially when using the "Project.Module.Procedure"
variant and parameters. I've found it so problematic that I frequently load the template
containing the macro I want to run as an AddIn and ensure that the procedure is global in
scope (Public). I then restrict usage to Application.Run "Procedure"
[,
params].
HTH + Cheers - Peter


"Chad Knudson" <[email protected]>, said:

I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.

In a module named modCustom in my Normal.dot template file I've added the
following two subs:

Public Sub TestCallParm(str As String)

MsgBox "This is a test. [" & str & "]"

End Sub

Public Sub TestSubWithParmCall()

Dim strTest As String

strTest = "Hello, World!"

Application.Run MacroName:="modCustom.TestCallParm",
varg1:=strTest
'
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails

End Sub

Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:

Public Sub TestCall()

MsgBox "This is a test."

End Sub

Public Sub TestCallNoParm()

Application.Run MacroName:="Normal.modCustom.TestCall" ' Succeeds

End Sub
 
C

Chad Knudson

Thanks for the suggestion about using the template as an AddIn. I tried
that and was able to call subs and functions passing parameters and it
worked like a champ.

Peter Hewett said:
Hi Chad Knudson

Application.Run can be very flakey, especially when using the "Project.Module.Procedure"
variant and parameters. I've found it so problematic that I frequently load the template
containing the macro I want to run as an AddIn and ensure that the procedure is global in
scope (Public). I then restrict usage to Application.Run "Procedure" [, params].

HTH + Cheers - Peter


I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.

In a module named modCustom in my Normal.dot template file I've added the
following two subs:

Public Sub TestCallParm(str As String)

MsgBox "This is a test. [" & str & "]"

End Sub

Public Sub TestSubWithParmCall()

Dim strTest As String

strTest = "Hello, World!"

Application.Run MacroName:="modCustom.TestCallParm", varg1:=strTest '
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails

End Sub

Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:

Public Sub TestCall()

MsgBox "This is a test."

End Sub

Public Sub TestCallNoParm()

Application.Run MacroName:="Normal.modCustom.TestCall" ' Succeeds

End Sub
 
W

Warrick

All you need to do is register your template!
Open the Visual Basic editor, Tools|References and add
your template.
If this helps maybe you can tell me if you can get
getfromclipboard and gettext running! I've given up!
Warrick
-----Original Message-----
I am having difficulty in trying to call a macro with parameters that
resides in another template file. While trying to track this down, I
simplified things to the code illustrated in the two subroutines I've
included in this message. If I include the project name in the MacroName
parameter to the Application.Run call, it fails with "Runtime Error 438:
Object doesn't support this property or method". If I remove the project
from the MacroName parameter to the Application.Run call, it successfully
runs.

In a module named modCustom in my Normal.dot template file I've added the
following two subs:

Public Sub TestCallParm(str As String)

MsgBox "This is a test. [" & str & "]"

End Sub

Public Sub TestSubWithParmCall()

Dim strTest As String

strTest = "Hello, World!"

Application.Run MacroName:="modCustom.TestCallParm", varg1:=strTest '
Succeeds
Application.Run MacroName:="Normal.modCustom.TestCallParm",
varg1:=strTest ' Fails

End Sub

Now, if I'm trying to call a sub without parameters it appears to work
properly as illustrated in the following two subs in the same module named
modCustom in Normal.dot:

Public Sub TestCall()

MsgBox "This is a test."

End Sub

Public Sub TestCallNoParm()

Application.Run
MacroName:="Normal.modCustom.TestCall" ' Succeeds
 

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