I try to develop a VBA code, with which Outlook will creat rules with a From- and a Category term.

The From-term is easy (ToOrFromRuleCondition) but the Category-term is troubling me a lot. Nevertheless I need both term in my rules. I would be very happy if someone has the solution for my problem.

main problem is in this part:

' not working yet, it is not creating the category "filing"

1
2
3
4
5
Set oCategoryCondition = oRule.Conditions.Category
With oCategoryCondition
.Enabled = True
.Categories.Add ("Filing")
End With

Here is my complete code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Sub Regelerstellung()


Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule
Dim colRuleActions As Outlook.RuleActions
Dim oMoveRuleAction As Outlook.MoveOrCopyRuleAction
Dim oFromCondition As Outlook.ToOrFromRuleCondition
Dim oCategoryCondition As Outlook.CategoryRuleCondition
Dim oExceptSubject As Outlook.TextRuleCondition
Dim oInbox As Outlook.Folder
Dim oMoveTarget As Outlook.Folder
Dim sTxt As String
Dim sTxt2 As String
Dim sTxt3 As String

'Asks for the folder
sTxt = InputBox("Folder:")
If sTxt = "" Then Exit Sub

'Asks for the subfolder
sTxt2 = InputBox("SubFolder:")

If sTxt2 = "" Then



Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)

Set oMoveTarget = oInbox.Folders(sTxt)

Else


Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)

Set oMoveTarget = oInbox.Folders(sTxt).Folders(sTxt2)

End If


'calls rules
Set colRules = Application.Session.DefaultStore.GetRules()

'calls sender
sTxt3 = InputBox("Sender")
If sTxt3 = "" Then Exit Sub

'creates new rule name
Set oRule = colRules.Create(sTxt3 + "rule", olRuleReceive)



'Creates the term "mail from sender"
Set oFromCondition = oRule.Conditions.From
With oFromCondition
.Enabled = True
.Recipients.Add (sTxt3)
.Recipients.ResolveAll
End With

'not working yet, it is not creating the category "Filing"
Set oCategoryCondition = oRule.Conditions.Category
With oCategoryCondition
.Enabled = True
.Categories.Add ("Filing")
End With

'creates the action MoveToFolder in the rule
Set oMoveRuleAction = oRule.Actions.MoveToFolder
With oMoveRuleAction
.Enabled = True
.Folder = oMoveTarget
End With

'saves rule

colRules.Save
End Sub
Thanks for your help