Edit Subject (Multiple Accounts)

Status
Not open for further replies.

dan1_uk

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
On my work PC I have two outlook accounts, my account and a shared account.

All external emails that we receive have an annoying note added to the subject line which is basically a warning that it's an external email.

I used to have a script set up with a rule but for some reason the 'run a script' option is no longer available under rules. I'm guessing it was an update but I have no admin rights on the machine.

I've now found some VBA code online that I used to remove this unwanted note from all emails as they arrive.
But unfortunately this only works for my email address, not the shared account.

The code I am using is:

Option Explicit

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
' instantiate objects declared WithEvents
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Item.subject = Replace(Item.subject, "CAUTION: External email - ", "")
Item.Save
Set Item = Nothing
End Sub


Any Ideas on how to make this work for both accounts?
 
I used to have a script set up with a rule but for some reason the 'run a script' option is no longer available under rules. I'm guessing it was an update but I have no admin rights on the machine.
Yeah, a security update removed it, it can be restored using a reg key.
Run-a-Script Rules Missing in Outlook

if you cant use the reg edit (or convince the admin to), you can convert the script to 'itemadd - that watches the folder and run it on all new mail. It can watch any folder, but you need to tell it to watch the folder....

Duplicate these lines, changing the bolded part to a new object name.
Private WithEvents olInboxItems As Items
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items

Copy the full macro, changing the object name
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)

To reference the other mailbox, Set sharedInboxItems = ***.Items, see
Working with VBA and non-default Outlook Folders
if its in as an account, you need to use the getfolderpath function or if its a in as a shared mailbox (notl isted in account settings), you need the shared code.

Because the macros are really short, duplicating you won't save code, but if it were longer, you could share the code:
Code:
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
RemoveWarning item
end sub

Private Sub sharedInboxItems_ItemAdd(ByVal Item As Object)
RemoveWarning item
end sub

Private Sub RemoveWarning(ByVal Item As Object)
On Error Resume Next
Item.subject = Replace(Item.subject, "CAUTION: External email - ", "")
Item.Save
Set Item = Nothing
End Sub
 
Yeah, a security update removed it, it can be restored using a reg key.
Run-a-Script Rules Missing in Outlook

if you cant use the reg edit (or convince the admin to), you can convert the script to 'itemadd - that watches the folder and run it on all new mail. It can watch any folder, but you need to tell it to watch the folder....

Duplicate these lines, changing the bolded part to a new object name.
Private WithEvents olInboxItems As Items
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items

Copy the full macro, changing the object name
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)

To reference the other mailbox, Set sharedInboxItems = ***.Items, see
Working with VBA and non-default Outlook Folders
if its in as an account, you need to use the getfolderpath function or if its a in as a shared mailbox (notl isted in account settings), you need the shared code.

Because the macros are really short, duplicating you won't save code, but if it were longer, you could share the code:
Code:
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
RemoveWarning item
end sub

Private Sub sharedInboxItems_ItemAdd(ByVal Item As Object)
RemoveWarning item
end sub

Private Sub RemoveWarning(ByVal Item As Object)
On Error Resume Next
Item.subject = Replace(Item.subject, "CAUTION: External email - ", "")
Item.Save
Set Item = Nothing
End Sub
Yeah, a security update removed it, it can be restored using a reg key.
Run-a-Script Rules Missing in Outlook

if you cant use the reg edit (or convince the admin to), you can convert the script to 'itemadd - that watches the folder and run it on all new mail. It can watch any folder, but you need to tell it to watch the folder....

Duplicate these lines, changing the bolded part to a new object name.
Private WithEvents olInboxItems As Items
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items

Copy the full macro, changing the object name
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)

To reference the other mailbox, Set sharedInboxItems = ***.Items, see
Working with VBA and non-default Outlook Folders
if its in as an account, you need to use the getfolderpath function or if its a in as a shared mailbox (notl isted in account settings), you need the shared code.

Because the macros are really short, duplicating you won't save code, but if it were longer, you could share the code:
Code:
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
RemoveWarning item
end sub

Private Sub sharedInboxItems_ItemAdd(ByVal Item As Object)
RemoveWarning item
end sub

Private Sub RemoveWarning(ByVal Item As Object)
On Error Resume Next
Item.subject = Replace(Item.subject, "CAUTION: External email - ", "")
Item.Save
Set Item = Nothing
End Sub


Thanks for this, I’ll take a look at it when I’m back at work on Monday.

Cheers!
 
I have now updated the code and can confirm it's working, thanks very much.
However I do have another issue.

I have a rule set up in Outlook to copy some emails from the shared account into a sub-folder in my account.
The rule is working but it copies the email before it has removed the unwanted text from the subject.

So the copied versions in my sub-folder still include the unwanted text.

Please see my code below:

Code:
Option Explicit

Private WithEvents olInboxItems As Items
Private WithEvents sharedInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session

Dim NS As Outlook.NameSpace
Dim objOwner As Outlook.Recipient

Set NS = Application.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("xxxxxx@xxxxxx.com")
objOwner.Resolve

' instantiate objects declared WithEvents
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set sharedInboxItems = NS.GetSharedDefaultFolder(objOwner, olFolderInbox).Items
End Sub




Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
RemoveWarning Item
End Sub

Private Sub sharedInboxItems_ItemAdd(ByVal Item As Object)
RemoveWarning Item
End Sub

Private Sub RemoveWarning(ByVal Item As Object)
On Error Resume Next
Item.subject = Replace(Item.subject, "CAUTION: External email - ", "")
Item.Save
Set Item = Nothing
End Sub

Thanks
 
Just one extra folder? You can watch that folder too.
Set olmysubfolderitems = objNS.GetDefaultFolder(olFolderInbox).folders("subbfolder").Items
(and all the other lines for olinboxitems)
However, if you have too many folders to watch, its unwieldy to watch each one.

Tip: if the folder is a subfolder of Inbox, use
dim olInbox as outlook.folder ' this may need to be up with the withevents lines

Set olInbox = objNS.GetDefaultFolder(olFolderInbox)
Set olInboxItems = olInbox.Items
Set olsubfolderItems = olInbox.Folders("subfolder").Items
 
Thanks again, this has worked a treat!
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
H using VBA to edit subject line Outlook VBA and Custom Forms 0
A Edit subject - and change conversationTopic - using VBA and redemption Outlook VBA and Custom Forms 2
R Edit Subject Line of Meeting Invitation? Using Outlook 1
P Edit Subject for every sent message Outlook VBA and Custom Forms 3
J Cannot edit a calendar event received as an invitation Using Outlook 2
E Edit incoming emails to remove a certain sentence added by the "system" Using Outlook 1
B Outlook 2019 Custom Email form - Edit default email form Outlook VBA and Custom Forms 6
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
Witzker Outlook 2019 Edit contact from email does not open the user defined contactform Using Outlook 3
T Outlook 2010 Cannot edit Calendar entries in OL 2010. Using Outlook 1
G print attachment straight away; working script edit not working Outlook VBA and Custom Forms 0
A Edit attachment Save and Reply Outlook VBA and Custom Forms 0
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
J Edit auto-complete list in Outlook 2016+/365? Using Outlook 0
J Message search and edit, another way? Outlook VBA and Custom Forms 4
L Wierd Office 365 Contact unable to edit body of random contacts Using Outlook 5
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
S Unable to Edit Contact Information in Certain Contact Folders Using Outlook 3
S Outlook.com cannot edit some contacts Using Outlook.com accounts in Outlook 5
B My outlook calendar edit issue Outlook VBA and Custom Forms 0
D VBA to edit body of incoming email and forwarding it Outlook VBA and Custom Forms 11
Diane Poremsky Edit and Save Outlook's Read-Only Attachments Using Outlook 0
P "Edit Message" feature removed in Outlook 2016 for Mac Outlook VBA and Custom Forms 0
snissen Where is "Edit Message" in Outlook 2016? Using Outlook 2
P People/Contact Record gets deleted when I edit it? Using Outlook 3
C Edit/Create Pen Not Working Outlook 2013 Using Outlook 1
X Bulk edit Contact forms Using Outlook 2
A Contact address disappears when opened to edit Using Outlook 5
J Can invitee edit details? Using Outlook 1
G Outlook 2013: Cannot Edit default Holiday Calendar Using Outlook 2
V Custom form won't open for edit Outlook VBA and Custom Forms 3
M Bulk Edit contact notes BCM (Business Contact Manager) 1
M Any way to edit the message field on a 2010 Outlook Form? Using Outlook 3
S OL 2010 shortcut to edit received mail Using Outlook 3
A Cannot edit or open existing contact nor add new ones. BCM (Business Contact Manager) 1
T Exchange 2010: 'could not save item' and 'Unknown error' when edit calendars Exchange Server Administration 1
M Outlook 2003: Cannot edit default Contact Form Using Outlook 11
S how can i edit email address/es from the from field Using Outlook 5
M How to edit a monthly calendar to remove the cover page Using Outlook 1
S How do I edit an attachment in an invite without notifying the attendees? Using Outlook 2
B Notice to user of appt. set by person with permission to edit (Outlook 2003) Using Outlook 7
H Edit email message Using Outlook 7
F Text Format Change - Bulk Edit Contact Notes Using Outlook 2
T Unable to edit Distribution Group membership via Outlook (works via ECP). Exchange Server Administration 7
D How do I edit a macro in Outlook? Outlook VBA and Custom Forms 1
E Currency Edit Control on custom Taskpanes Outlook VBA and Custom Forms 1
E Currency Edit Control on custom Taskpanes Outlook VBA and Custom Forms 1
A edit right pane of mail Outlook VBA and Custom Forms 15
S Correct way to Identify is Mail is opened in Edit Mode Outlook VBA and Custom Forms 1
F Auto changing email subject line in bulk Using Outlook 2

Similar threads

Back
Top