假设我们创建一个约会并将其命名为“脚本运行”。
我们设置了它应该运行的时间(您可以添加 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