Add a string to the conditions in .Conditions.BodyOrSubject.Text

Post number 3 has been selected as the best answer.

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?

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
 

BartH

Member
Outlook version
Outlook 2016 64 bit
Email Account
POP3
ThnX Diane,

Tried that, but doesn't work - type mismatch.

Then figured out
Code:
oRule.Conditions.BodyOrSubject.Text
is an array
(I could have know before, as this works:
Code:
For Each co In colRules.Item("Spam").Conditions.BodyOrSubject.Text
and
Code:
TypeName(colRules.Item("Spam").Conditions.BodyOrSubject.Text) gives String()
)

so this this altercation to the code is the solution:

Code:
    varRuleArr = oRule.Conditions.BodyOrSubject.Text
    
    ReDim Preserve varRuleArr(UBound(varRuleArr) + 1)
    varRuleArr(UBound(varRuleArr)) = strSubject
    oRule.Conditions.BodyOrSubject.Text = varRuleArr
    colRules.Save
    MsgBox strSubject & vbCr & " added to this rule.", vbInformation, "Spam"
 
Similar threads
Thread starter Title Forum Replies Date
P Add inanimate objects to meetings? Using Outlook 1
O Outlook 2010 Add delete button to the side of the message list Using Outlook 1
A "Get Add-Ins" - Which Version of Outlook to use Using Outlook 1
D Do I need Exchange Add-In? Using Outlook 6
C-S-R Manage Add-ins (Remove Wunderlist) Using Outlook 6
A iCloud add in problems Using Outlook 4
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 8
C Looking for feedback on new Outlook Add-in Using Outlook 0
L isn't there an OL add-on that flags addressee before sending Using Outlook 3
S Add VBA save code Using Outlook 0
P Shortcut Pane - add shortcut to Office365 group mailbox Using Outlook 1
B Add ComboBox Value to Body of Email Outlook VBA and Custom Forms 1
G How to add a folder shortcut to outlook quick access toolbar? Using Outlook 6
G Add to Outlook Contacts - Point to non-default contacts folder Using Outlook 0
M Automatically add senders first name to a greeting Outlook VBA and Custom Forms 1
C Add Form to Appointments Received, Automatically Outlook VBA and Custom Forms 6
O Outlook tasks - Add text column with multiple lines Using Outlook 3
W April 2020 Office 365 Update - Add-Ons fail after Office 365 Update Using Outlook 6
B Task Filter Not Working When I add too many criteria Using Outlook 0
D Add date next to day name in Outlook Today calendar view Using Outlook 1
D iCloud Add-in not working in Outlook 2013 and Outlook 2016 After Windows Upgrade & iCloud Upgrade Using Outlook 2
P Add Paste Unformatted to QAT Using Outlook 1
M Ignore slow add-ins Using Outlook 0
B Add Prefix text to Subject Line Using Outlook 1
Jennifer Murphy Add birthdays to calendar Using Outlook 7
Z Add text to auto-forwarded e-mail Outlook VBA and Custom Forms 4
P Add, remove, & reorder folder pane Using Outlook 6
L Tired of Outlook disabling Adobe PDF Creator add-in Using Outlook 1
R Add 'Company' to Select Names Form Using Outlook 1
A Outlook 2016 Web add-in missing on some machines Using Outlook 9
P Syncing problems with add-ins Using Outlook 3
Mark Foley Cannot enable add-in in outlook 2010 Using Outlook 0
BretAB Is it possible to add a lookup field to a Message form? Outlook VBA and Custom Forms 4
J Add an Attachment Using an Array and Match first 17 Letters to Matching Template .oft to Send eMail Outlook VBA and Custom Forms 2
iwshim outlook 2013 - I cannot see the "Manage Add-ins" Using Outlook 2
A Add multiple servers "on behalf of" email to "safe senders" list. Using Outlook 1
S Add Exchange Account as Secondary to Existing PST? Exchange Server Administration 1
C Macro to add multiple recipients to message Outlook VBA and Custom Forms 3
A Add to Outlook Contacts from email - default view Outlook VBA and Custom Forms 1
P Add a contact to the New Task in Outlook 2016 Using Outlook 2
e_a_g_l_e_p_i Is there a way to add something that is in the "Format Text" tab to the "basic Text" on the message tab Using Outlook 1
B When working on emails in a certain folder, when I hit reply or reply all, I would like it re always reply all and add an email address to send to Outlook VBA and Custom Forms 3
M Macro to add date/time stamp to subject Outlook VBA and Custom Forms 4
O Outlook on Android - add BCC Using Outlook 2
J Execute Add-In Button from VBA Outlook 2016 Outlook VBA and Custom Forms 1
S Example VBA Macro - To Conditionally Change the From Account and Add a BCC Address on Emails Outlook VBA and Custom Forms 11
B When I add more search strings to RULES, it is not processing them Using Outlook 3
D Add Tetxbox at form open Outlook VBA and Custom Forms 1
Andrew Quirl Open attachment, manipulate without add-on program? Outlook VBA and Custom Forms 5
B Searching for a Add-In Using Outlook 3

Similar threads

Top