0

我在 Outlook 上的宏有问题。

我目前正在尝试通过批处理调用 Outlook 并将其作为参数传递给我通过在批处理中设置的环境变量获得的宏的名称。但是,我确实获得了宏的名称,但是该过程在调用函数时停止。有人能告诉我正确的方法吗?

VBA ThisOutlookSession

Private Sub Application_Startup()
    Dim strMacroName As String
    strMacroName = CreateObject("WScript.Shell").Environment("process").Item("MacroName")
    'MsgBox strMacroName
    'MsgBox VarType(strMacroName)
    If strMacroName <> "" Then Call strMacroName
    End Sub

VBA 模块

Option Explicit
Sub macro1()
MsgBox "macro1"
End Sub
Sub macro2()
MsgBox "macro2"
End Sub

Set WorkingPath=C:\Temp\Outlook
Set MacroName=%1
start OUTLOOK.EXE
Set MacroName=
Set WorkingPath=

结果

在此处输入图像描述

4

1 回答 1

1

这里有几个方面... 第一点是处理 Outlook 时可能存在的安全问题。您可以在 Outlook 对象模型的安全行为一文中了解更多相关信息。

另一点是您可以ThisOutlookSession通过以下方式调用模块中声明的 VBA 宏(例如,从任何其他 Office 应用程序):

Sub test()
  Dim OutApp As Object
  Set OutApp = CreateObject("Outlook.Application")
  OutApp.Session.Logon
  OutApp.HelloWorld
End Sub

在模块中以以下方式HelloWorld声明子的位置:ThisOutlookSession

Option Explicit
Public Sub HelloWorld()
  MsgBox "Hello world !!"
End Sub

请注意,您可以从模块中调用任何ThisOutlookSession模块。无需直接访问其他模块。

于 2021-12-21T15:06:45.807 回答