(
Anhgolden's Blog)-Sưu tầm
Trong ms outlook có tính năng "
Rules and Alerts" nhằm thiết lập Rule cho Email. Chọn check hoặc uncheck vào Rule nếu muốn Enable hoặc Disable Rule. Tuy nhiên, ms outlook không cho phép chúng ta muốn thiết lập chế độ tự động Enable hoặc Disable Rule theo thời gian. Muốn đáp ứng nhu cầu trên, xin chia sẻ bài viết hướng dẫn thực hiện bằng VBA kết hợp với chế độ Reminder có sẵn của ms outlook.
Lưu ý: Chương trình chỉ chạy khi Outlook được mở.
Bước 1: Hãy tạo Task với subject là "
Disable Rule", "
Enable Rule" và thiết lập Reminder time.
Bước 2: Tạo Script sau
Bấm Alt+F11 để mở VBA Editor và paste đoạn code sau vào phần "ThisOutlookSession".
Private Sub Application_Reminder(ByVal Item As Object)
If Item.MessageClass <> "IPM.Task" Then
Exit Sub
End If
If Item.Subject = "Enable Rule" Then
Enable_Run_Rule
End If
If Item.Subject = "Disable Rule" Then
Disable_Run_Rule
End If
End Sub
' Macro to enable a rule
Sub Enable_Run_Rule()
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Dim intCount As Integer
Dim blnExecute As Boolean
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("Your Rule Name")
olRule.Enabled = True
If blnExecute Then olRule.Execute ShowProgress:=True
olRules.Save
Set olRules = Nothing
Set olRule = Nothing
End Sub
' Macro to disable a rule
Sub Disable_Run_Rule()
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Dim intCount As Integer
Dim blnExecute As Boolean
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("Your Rule Name")
olRule.Enabled = False
If blnExecute Then olRule.Execute ShowProgress:=True
olRules.Save
Set olRules = Nothing
Set olRule = Nothing
End Sub
Trường hợp muốn thiết lập cho nhiều Rule cùng lúc
Chúng ta có thể áp dụng cho nhiều Rule, tuy nhiên trong trường hợp này phải chỉ định (assign) Rule cho
Categories đặt tên là "Enable Rule" hoặc "Disable Rule" cho từng Task.
Private Sub Application_Reminder(ByVal Item As Object)
Dim strRule As String
If Item.MessageClass <> "IPM.Task" Then
Exit Sub
End If
strRule = Item.Subject
If Item.Categories = "Enable Rule" Then
Run_Rule strRule, 1
End If
If Item.Categories = "Disable Rule" Then
Run_Rule strRule, 0
End If
End Sub
Function Run_Rule(rule_name As String, tf As Boolean) As Boolean
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Dim blnExecute As Boolean
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item(rule_name)
olRule.Enabled = tf
If blnExecute Then olRule.Execute ShowProgress:=True
olRules.Save
Set olRules = Nothing
Set olRule = Nothing
End Function
Nguồn:
http://www.slipstick.com/developer/enable-or-disable-rule-using-reminder-and-vba/
Read More »