Application.Run

This can be used to run a macro written in Visual Basic or the Microsoft Excel macro language, or to run a function in a DLL or XLL.

Application.Run(Macro, Arg1, Arg2, Arg3, ..) 

You cannot use named arguments with this method. Arguments must be passed by position.
You must pass all the parameters as separate arguments and not as one single string (for example Application.Run("Macro(Arg1,Arg2,Arg3)")


If the datatype of any of your paramaters (or of the returned datatype) is not declared then these items are assumed to have the Variant datatype.
All the arguments are converted to values which means you cannot pass any objects
Any objects passed as arguments to a macro will be converted to a value (by applying the Value property to the object).

mySum = Application.Run("MYCUSTOM.XLM!My_Func_Sum", 1, 5)
 


Procedure name assigned to a Variable

Sub MainProc() 
Dim sprocname as String
   Select Case WeekDay(Now())
      Case 0: sprocname = "Weekend"
      Case 1: sprocname = "Weekend"
      Case Else: sprocname = "Daily"
   End Select
   Application.Run sprocname
End Sub

Sub Weekend()
End Sub

Sub Daily()
End Sub


Calling Procedures in different files

This can be done in two ways.

1) Create a reference to the other file. To create a reference (Tools > References).

After a reference has been established you can call procedures in other files.
The referenced file does not have to be open and is treated like an object library.

2) Use the Application.Run method and specify the filename and folder path of the other file.


Word

Runs a Visual Basic macro.
You can pass up to 30 parameter values to the specified macro.
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.

Application.Run MacroName:="Normal.Module2.Macro1" 

Application.Run "Normal.Module1.MAIN"


This assumes the VBA Project has been changed to MyProject. This is rarely changed.

Application.Run "MyProject.MyModule.MyProcedure" 


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


Include an example with a folder location as well !!
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"



© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext