【问题标题】:Use calendar appointment in outlook to trigger VBA macro在 Outlook 中使用日历约会来触发 VBA 宏
【发布时间】:2020-09-23 20:00:37
【问题描述】:

如何在 Outlook 中设置约会以使其通过约会提醒触发 VBA 宏? 就我而言,我希望 Outlook 被安排在某个时间打开一个 excel 文件。

有一些示例,但没有一个符合我的要求,因为大多数使用 Outlook 任务而不是约会。

例如: https://www.slipstick.com/developer/code-samples/running-outlook-macros-schedule/ 还有这个Outlook and Excel VBA task Scheduler

【问题讨论】:

    标签: vba outlook task appointment


    【解决方案1】:

    假设我们创建一个约会并将其命名为“脚本运行”。

    我们设置了它应该运行的时间(您可以添加 Recurrence)并且不要忘记选择提醒!

    同时创建一个类别并为其命名。

    然后我使用修改后的代码,将其粘贴到“ThisOutlookSession”中:

    要粘贴到“ThisOutlookSession”中的代码

    'The Private subs go in ThisOutlookSession
    'declare this object withEvents displaying all the events
    'might need to access library "Microsoft Excel 16.0 Object Library"
    
    Private WithEvents olRemind As Outlook.Reminders
    
    Private Sub Application_Reminder(ByVal Item As Object)
    
    Set olRemind = Outlook.Reminders
    
    If Item.MessageClass <> "IPM.Appointment" Then
        Exit Sub
    End If
    
    If Item.Categories <> "Run weekly script updates" Then 'Add another If statement to add additional appointments
        Exit Sub
    End If
    
    Call ExecuteFile ' call sub
    
    End Sub
    
    Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
    
    'This is to dismiss the reminder
    
    For Each objRem In olRemind
            If objRem.Caption = "Script Run" Then
                If objRem.IsVisible Then
                    objRem.Dismiss
                    Cancel = True
                End If
                Exit For
            End If
        Next objRem
    End Sub
    

    为了触发打开 excel 文件,我们使用了一个位于“Module1”的子程序。 它看起来像这样:


    版本 1:

    Sub ExecuteFile()
    
    
    Call Shell("G:\Till\Budget script.exe", vbNormalFocus) 'To call an external program
    
    'Run Excel macro
    Dim xlApp As Excel.Application
    Dim xlBook As Workbook
    
    Set xlApp = New Excel.Application
    Set xlBook = xlApp.Workbooks.Open("G:\Till\Budget.xlsm") ' update with Excel name
    xlApp.Visible = True
    
    '// Run Macro in Excel_File
    xlBook.Application.Run "Module1.CheckDates"
    
    Set xlApp = Nothing
    Set xlBook = Nothing
    
    End Sub
    

    版本 2(后期绑定):

    当您的授权访问权限有限时,这对工作很有用。

    Sub ExecuteFile()
    
    Call Shell("G:\Till\Budget script.exe", vbNormalFocus) 'To call an external program
    
    'Run Excel macro
    Dim xlApp As Object
    Dim xlBook As Workbook
    Dim blnEXCEL As Boolean
    
    
    'Establish an EXCEL application object, by Ken Snell
    'https://social.msdn.microsoft.com/Forums/office/en-US/81d29bf1-524c-4303-8101-611cc30d739b/using-excel-objects-via-late-binding?forum=accessdev
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
          Set xlApp = CreateObject("Excel.Application")
          blnEXCEL = True
    End If
    Err.Clear
    On Error GoTo 0
    
    
    Set xlBook = xlApp.Workbooks.Open("G:\Till\Budget.xlsm") ' update with Excel name
    xlApp.Visible = True
    
    '// Run Macro in Excel_File
    xlBook.Application.Run "Module1.CheckDates"
    
    Set xlApp = Nothing
    Set xlBook = Nothing
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      相关资源
      最近更新 更多