如何自动运行 Outlook 宏?

如何自动运行 Outlook 宏?

我已经开发了一个 Outlook 宏,并希望这个宏能够自动运行,无需用户的任何提示或邮件,无论 Outlook 是否打开。

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
 bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12
Private Const SW_SHOWMAXIMIZED = 3
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_MAXIMIZE = 3
Private Const SW_MINIMIZE = 6
Private Const SW_NORMAL = 1
Private Sub Application_Reminder(ByVal Item As Object)

If TypeName(Item) = "TaskItem" Then
    Dim myItem As TaskItem
    Set myItem = Item
    If myItem.Subject = "run macro Mail_workbook_Outlook_1" Then

        Call Mail_workbook_Outlook_1   '...your macro name here

    End If
End If
End Sub
Sub Mail_workbook_Outlook_1()
    ChDrive ("D:")
    ChDir ("D:\Eclipse\Workspaces\struts\Sonar\src\report")

    'Shell ("cmd.exe /S /K" & "java D:\Eclipse\Workspaces\struts\Sonar\src\reportOpenBrowser")

    'Const exeCmd = "java OpenBrowser"
    'Shell ("java OpenBrowser")

    Dim sdkCommand As String

    sdkCommand = "java Orange "
    Shell ("cmd.exe /c" & sdkCommand)

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "check"
        .CC = ""
        .BCC = ""
        .Subject = "Orange "
        .Body = "Hi all " _
                & "PFB the status of Sonar report for Trunk"
        .Attachments.Add ActiveWorkbook.FullName
        'You can add other files also like this
        .Attachments.Add ("d:\\Orange .jpg")
        .HTMLBody = .HTMLBody & "<br>Orange REPORT:<br>" _
                & "<br><img src='d:\\Orange .jpg'" & "width='900' height='600'><br>" _
                & "Orange .<br>" _
                & "google.com" _
                & "<br>Regards,<br>Gaurav</font></span>"
        .Send   'or use .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

答案1

您可以使用/autorun开关:

outlook.exe /autorun <macroname>

例如,在 Visual Basic 编辑器 ( Alt+ F11) 中创建以下内容:

Sub Hello()
    MsgBox "Hello World!"
End Sub

在桌面上创建一个快捷方式,其目标为:

"C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE"  /autorun hello

双击快捷方式,启动 Outlook 时您将收到一条消息。

请注意,路径outlook.exe可能因您的版本和安装而异。


您还可以定义Application_Startup每次 Outlook 启动时运行的宏:

Private Sub Application_Startup() 

    MsgBox "Welcome, " & Application.GetNamespace("MAPI").CurrentUser 

    Application.ActiveExplorer.WindowState = olMaximized 

End Sub

以上内容来自Microsoft 开发人员参考网站

您可能需要配置您的安全设置才能使其正常工作:

File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings -> Enable All

请注意,我没有 Outlook 2010 来确认上述内容:-)

答案2

这是不可能的。如果不运行 Outlook,您就无法运行 Outlook 宏,就像如果没有 CD 播放器,您就无法播放 CD 一样。

相关内容