• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Outlook rule run a Script doesn't work

bibitux

New Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
#1
hello everyone,
Sorry for my english.
i found the script to change the subject of mails. The script runs over a rule. Unfortunately the script only works in an email where it is active.
I have 10 emails in a test folder. Rest of the mails does not work.
I chose by rule: Apply rule to all messages.
Can someone help me find out why the rest of the emails don't work.
Thank you.

Public Sub EditSubject(Mail As Outlook.MailItem)
Dim Item As Outlook.MailItem
Dim oInspector As Inspector
Dim strSubject As String
Dim strPrefixSubject As String

Set oInspector = Application.ActiveInspector

If oInspector Is Nothing Then
Set Item = Application.ActiveExplorer.Selection.Item(1)
Else
Set Item = oInspector.CurrentItem
End If

strSubject = "AW: "

Debug.Print Item.subject

Item.subject = Replace(Item.subject, strSubject, "", vbTextCompare)
Item.Save

' Prefix subject with Confidential and Legally Privileged
strPrefixSubject = "" & Item.subject

' Set the message subject
Item.subject = strPrefixSubject
Item.Save


Set Item = Nothing
Set oInspector = Nothing

End Sub
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#2
This line:
Set Item = Application.ActiveExplorer.Selection.Item(1)
Says use it on the selected message.

Remove this entire block - it applies to open or selected messages, not to new messages arriving
Set oInspector = Application.ActiveInspector

If oInspector Is Nothing Then
Set Item = Application.ActiveExplorer.Selection.Item(1)
Else
Set Item = oInspector.CurrentItem
End If


Change Mail to Item in the first line and delete these two Dim lines
Public Sub EditSubject(Mail As Outlook.MailItem)
Dim Item As Outlook.MailItem
Dim oInspector As Inspector


Delete this line - Set oInspector = Nothing


Code:
Public Sub EditSubject(Item As Outlook.MailItem)
Dim strSubject As String
Dim strPrefixSubject As String

strSubject = "AW: "

Debug.Print Item.subject

Item.subject = Replace(Item.subject, strSubject, "", vbTextCompare)
Item.Save

' Prefix subject with Confidential and Legally Privileged
strPrefixSubject = "" & Item.subject

' Set the message subject
Item.subject = strPrefixSubject
Item.Save

Set Item = Nothing
End Sub
 

Similar threads