You can run a macro from a VBE add-in without using the Application.Run method by using the ExecuteExcel4Macro method.
Here's an example:
using Microsoft.Office.Interop.Excel; public static void RunMacro(string macroName) { Application xlApp = (Application)Marshal.GetActiveObject("Excel.Application"); object result; xlApp.ExecuteExcel4Macro(macroName, out result); } In this example, we are first getting a reference to the running instance of Excel using the Marshal.GetActiveObject method.
Then, we are using the ExecuteExcel4Macro method to run the macro. The first argument is the name of the macro to run, and the second argument is an object that will contain the result of the macro (if any).
Note that the ExecuteExcel4Macro method is designed to work with Excel 4.0 macros, which are the older style macros that were used in Excel before the introduction of VBA. If you want to run a VBA macro, you will need to modify the code to use the Application.Run method.
"VBE add-in run macro programmatically"
ThisWorkbook.VBProject.VBComponents("Module1").CodeModule.ExecuteLines "MyMacro" "VBA execute macro from VBE add-in"
Application.VBE.VBProjects("MyAddIn").VBComponents("Module1").CodeModule.ExecuteLines "MyMacro" "VBE add-in run macro without Application.Run"
Call MyAddInModule.MyMacro
"Execute VBA macro in VBE add-in"
Call Application.VBE.VBProjects("MyAddIn").VBComponents("Module1").CodeModule.ProcOfLine("MyMacro", vbext_pk_Proc) "Run VBE add-in macro without Run method"
ThisWorkbook.VBProject.VBComponents("Module1").RunLines "MyMacro" "Execute VBE add-in macro programmatically"
Call Application.VBE.VBProjects("MyAddIn").VBComponents("Module1").CodeModule.RunProc("MyMacro") "VBE add-in run subroutine without Application.Run"
Call MyAddInModule.MySubroutine
"VBA execute VBE add-in macro from another module"
ThisWorkbook.VBProject.VBComponents("Module2").CodeModule.ExecuteLines "MyAddInModule.MyMacro" "VBE add-in call macro without Application.Run"
Call Application.VBE.VBProjects("MyAddIn").VBComponents("Module1").CodeModule.ProcOfLine("MyMacro", vbext_pk_Proc).ProcBodyLine(1, 1) "VBA run macro in VBE add-in without Run method"
ThisWorkbook.VBProject.VBComponents("MyAddInModule").CodeModule.RunProc "MyMacro" for-xml mini-css-extract-plugin fastapi scramble youtube-analytics database-connectivity android-camerax scilab angularjs qr-code