BartH
Member
- Outlook version
- Outlook 2016 64 bit
- Email Account
- POP3
I use multiple rules that use conditions like BodyOrSubject holds "string1; string2 etc"
I can manually add a string to the BodyOrSubject conditions, but now want to do that with VBA.
The code below runs fine until "oRule.Conditions.BodyOrSubject.Add strSubject" - what would be the correct syntax there?
additional function:
I can manually add a string to the BodyOrSubject conditions, but now want to do that with VBA.
The code below runs fine until "oRule.Conditions.BodyOrSubject.Add strSubject" - what would be the correct syntax there?
Code:
strSubject = InputBox("Check this subject text... (only keep what is significant).", "Subject", Replace(Trim(GetSelectedItemSubject), ";", ""))
If strSubject = "" Then Exit Sub
Set colRules = Application.Session.DefaultStore.GetRules()
Set oRule = colRules.Item("Spam")
For Each co In colRules.Item("Spam").Conditions.BodyOrSubject.Text
' Debug.Print co
If co = strSubject Then
MsgBox strSubject & vbCr & " allready exists in this rule.", vbInformation, "Spam"
Exit Sub
End If
Next co
oRule.Conditions.BodyOrSubject.Add strSubject
MsgBox strSubject & vbCr & " added to this rule.", vbInformation, "Spam"
colRules.Save
additional function:
Code:
Function GetSelectedItemSubject() As String
MsgTxt = ""
Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.selection
For x = 1 To myOlSel.Count
If myOlSel.Item(x).Class = OlObjectClass.olMail Then
' For mail item, use the SenderName property.
Set oMail = myOlSel.Item(x)
If MsgTxt = "" Then
MsgTxt = oMail.Subject & ";"
Else
MsgTxt = MsgTxt & " " & oMail.Subject & ";"
End If
ElseIf myOlSel.Item(x).Class = OlObjectClass.olAppointment Then
' For appointment item, use the Organizer property.
Set oAppt = myOlSel.Item(x)
If MsgTxt = "" Then
MsgTxt = oMail.Subject & ";"
Else
MsgTxt = MsgTxt & " " & oMail.Subject & ";"
End If
Else
' For other items, use the property accessor to get sender ID,
' then get the address entry to display the sender name.
Set oPA = myOlSel.Item(x).PropertyAccessor
strSenderID = oPA.GetProperty(PR_SENT_REPRESENTING_ENTRYID)
Set mySender = Application.Session.GetAddressEntryFromID(strSenderID)
If MsgTxt = "" Then
MsgTxt = oMail.Subject & ";"
Else
MsgTxt = MsgTxt & " " & oMail.Subject & ";"
End If
End If
Next x
GetSelectedItemSubject = MsgTxt
End Function