Outlook 2010 VBA runs a macro whose name was passed as a parameter

I work in Outlook 2010 on Windows 7, writing to VBA, and I want to pass the name of the macro (or subroutine) as a string variable to another subroutine and run this macro. In Word, you can do this with Application.Run MacroName: = strMacroName Where strMacroName is a string variable with a macro name. This approach does not work in Outlook 2010. How can I accomplish the same thing? I tried

  • Call Application.strMacroName
  • Call strMacroName
  • strMacroName in its line
  • Outlook.Application.strMacroName

None of these things worked.

I just upgraded to Outlook 2010 and therefore can no longer use keyboard shortcuts to run custom code for email processing. Therefore, in order to restore some version of this functionality, I created code to present a dialog box with my most common macros. The code is pretty clean to change over time and pass the name of the macro that I want to run, but I used to run this procedure in one command ( Application.Run MacroName:=strMacroName ).

Now I have to include a long switch statement to do the same. Not so easy.

Thanks!

+4
source share
2 answers

CallByName seems the only way.

Using this code in ThisOutlookSession :

 Public Sub TestFoo() Dim testClass As New TestClass1 CallByName testClass, "TestMethod1", VbMethod End Sub 

And this code in TestClass1 :

 Public Sub TestMethod1() MsgBox "huzzah!" End Sub 

Calling ThisOutlookSession.TestFoo gives the expected message box.

+6
source

As far as I can tell, the only way to run the named macro programmatically in Outlook (without using custom classes like the other answer here) is to create a temporary CommandBarButton, execute it, and delete it immediately. This works in Outlook 2013 even with the ribbon:

 Public Sub runProc(procToRun As String) With Application.ActiveExplorer.CommandBars.Add("Custom", temporary:=True) With .Controls.Add(msoControlButton, 1, , , True) .OnAction = procToRun .Execute .Delete End With .Delete End With End Sub 

I know this is an old question, but I could not find this exact answer myself at the end of 2017, so hopefully this will help someone else. Note that this does NOT run macros that are in ThisOutlookSession ... your macro must be in a code module.

0
source

Source: https://habr.com/ru/post/1401079/


All Articles