本文介绍了在Outlook 2010 VBA中运行一个宏,该宏的名称作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7中用VBA编写的Windows 7中的Outlook 2010,想要将宏(或子例程)的名称作为字符串变量传递给另一个子例程,并让该例程运行宏.在Word中,您可以使用Application.Run宏名称:= strMacroName其中strMacroName是带有宏名称的字符串变量.该方法在Outlook 2010中不起作用.如何完成同一件事?我尝试过

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

  1. Call Application.strMacroName
  2. Call strMacroName
  3. strMacroName单独一行
  4. Outlook.Application.strMacroName
  1. Call Application.strMacroName
  2. Call strMacroName
  3. strMacroName on its own line
  4. Outlook.Application.strMacroName

这些都不起作用.

我刚刚升级到Outlook 2010,因此不能再使用键盘快捷键来运行用于处理电子邮件的自定义代码.因此,为了还原该功能的某些版本,我创建了代码以显示一个包含我最常用的宏的对话框.随着时间的流逝并传递我想要运行的宏的名称,该代码相当干净,可以修改,但是我过去能够用一个命令(Application.Run MacroName:=strMacroName)运行该例程.

I just upgraded to Outlook 2010 and so can no longer use keyboard shortcuts to run custom code for handling email. So to restore some version of that functionality I have created code to present a dialog box with my most common macros. The code is fairly clean to modify as time goes along and pass along the name of the macro I want to run but I used to be able to run that routine in one command (Application.Run MacroName:=strMacroName).

现在我必须包含一个很长的switch语句才能完成同样的事情.没有那么简单.

Now I have to include a long switch statement to accomplish the same thing. Not nearly as simple.

谢谢!

推荐答案

CallByName似乎是唯一的方法.

ThisOutlookSession中使用以下代码:

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

TestClass1中的这段代码:

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

呼叫ThisOutlookSession.TestFoo给您期望的消息框.

Calling ThisOutlookSession.TestFoo gives you the expected message box.

这篇关于在Outlook 2010 VBA中运行一个宏,该宏的名称作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:49