Batch adding text to email subject lines in Outlook

Status
Not open for further replies.

paulkaye

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
At our firm, we put file numbers in all communications with our clients. However, we frequently begin dialogues with potential clients before opening a file for them. Then, once they become a client and we open a file, we have to go back and manually change each un-numbered email subject line. It's normally pretty easy to come up with a search term that finds all the emails that need changing, but it's time-consuming to change each one manually. Is there a way to batch insert a string that looks like "[12345]" (without quotes) into the subject lines of all the emails in a search result and/or folder? It's not very important if the string is inserted at the beginning or end of the subject line.

Many thanks in advance,

Paul
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Actually, I do have some VBA... i repurposed the code here: http://www.slipstick.com/outlook/calendar/copy-is-prefixed-meeting-subject/ - i would create a search folder to find the messages then run it on the search folder and remove the If Mid line and the first End if. You can calendar.items to mail.items too. :)

Sub RemoveCopy()

Dim myolApp As Outlook.Application

Dim calendar As MAPIFolder

Dim aItem As Object

Set myolApp = CreateObject("Outlook.Application")

Set calendar = myolApp.ActiveExplorer.CurrentFolder

Dim iItemsUpdated As Integer

Dim strTemp As String

iItemsUpdated = 0

For Each aItem In calendar.Items
If Mid(aItem.Subject, 1, 4) = "New " Then
strTemp = "[12345]" & aItem.Subject
aItem.Subject = strTemp
iItemsUpdated = iItemsUpdated + 1
End If
aItem.Save

Next aItem

MsgBox iItemsUpdated & " of " & calendar.Items.Count & " Messages Updated"

End Sub
 

paulkaye

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Thank you for helping me out. Unfortunately, I think I need more explanation. I am not familiar with VBA code, so don't understand what I should do with what you provided me. Is VBA the language in which macros are written? Should I just paste your code into the VBA editor? Then how do I run it on a given folder?

Sorry - I really do need a lot more explanation!

Thanks again in advance,

Paul
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
yes, paste it into the VBA editor (Alt+F11 to open the VB editor) then press Run. Note: if any line is shown in red, it contains an error. smart quotes are the usual cause. Blue, green, blakc text is ok.

You'll also need macro security set on medium or sign the code. See How to use VBA Editor for more information on macro security and signing code.

I tidied the code up a little and added an input box to make it easier - the brackets are added automatically so you just need to enter the file number. This will work on all messages in a search folder. include sent messages.

Sub AddFileNumber()

Dim myolApp As Outlook.Application

Dim calendar As MAPIFolder

Dim aItem As Object

Set myolApp = CreateObject("Outlook.Application")

Set mail = myolApp.ActiveExplorer.CurrentFolder

Dim iItemsUpdated As Integer

Dim strTemp As String

Dim strFilenum As String

strFilenum = InputBox("Enter the filenumber")

iItemsUpdated = 0

For Each aItem In mail.Items
strTemp = "[" & strFilenum & "] " & aItem.Subject
aItem.Subject = strTemp
iItemsUpdated = iItemsUpdated + 1
aItem.Save

Next aItem

MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"

End Sub
 

paulkaye

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Hi Diane,

Thank you for going to the trouble of tidying up the code, creating the file number entry box (amazing!) and 'holding my hand' through using it. It seems to work! (sorry if I sound surprised - I'm just always amazed at how code actually does what it's meant to!).

Two issues:

1) In my original post, I said that I could easily generate a search term to pick up all the mails that I wanted. However, I see that this is not as easy in search folders. For example, boolean is not supported (it seems like the default is AND). I saw a post you wrote somewhere else that refers to http://www.outlook-tips.net/outlook-2010/tip-187-using-querybuilder, for using Query Builder, but that post does't fully explain how to enter the registry entry. I've put a comment there, and am hoping for a response, but if you can explain this, too, great!

2) Not all the emails in the search folder are 'treated'! I ran the macro and it only added the file number to a proportion (~half) of the emails. I had to run it four times to treat all the emails (about 30 in total in this case). Each time I received a message telling me that "x out of y" emails had been renamed. However, x was the number renamed, and y was the number remaining (not the original number of mails).

Can you help?

Thanks once again,

Paul
 

paulkaye

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Ok, I've answered question 1. I didn't realise that simply adding the key with no more info would switch on Query Builder. I think question 2 still stands, though!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
RE: Query builder for anyone finding the thread - i have a reg files at Using Outlook's QueryBuilder - download and double click to set it.

#2. I didn't see that in Outlook 2010. The only weirdness was related to conversation views, which is (mostly) new to outlook 2010. The Conversation subject (which is in the message header) will not be changed, only the message subject - this holds true for all versions of outlook, however you'll only notice this in older versions if you view the header or use a Conversation view.

Were the first messages skipped when you re-ran it? (Or were they removed from the search folder?)

Are the messages all in one folder? I think my test messages were all in two folders.

What sort order do you use? It shouldn't matter... but your comment about 'half' has me wondering if its related to loops. Code that uses Loops to step through messages will often skip everiy other message unless you count backwards. This code doesn't use that type of loop though.

(I'll run it in Outlook 2007 and see if i can repro.)
 

paulkaye

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
When you ask "how many folders", I'm assuming you mean 'real' folders - they were all in the same search folder when I ran the macro. The emails were in at least 3 folders (inbox, deleted and sent, at the least) - I don't use folders for 'filing' emails, though, so I imagine 3 is exactly how many folders there were. Perhaps I was wrong, and I only had to run the macro 3 times. I didn't notice what folders the treated/untreated emails were in.

My sort order is normally received, newest first (although I can't say for sure what it was in the search folder at the time of running the macro).

I'm not using (and never use) conversation view, grouping, etc.

Thanks for your perseverence! FYI - running the macro 3 times is still much better than renaming 50 emails!

Paul
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Yes, how many real folders were the messages in the search folders in... if you don't file mail, those 3 should be it.

It should like you view and setup is similar to mine.
 

paulkaye

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Hi Diane,

So I'll leave it for now, and next time I need the macro, I'll pay attention to what's happening and come back to this post.

Thanks again for all your help!

Paul
 

John

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
yes, paste it into the VBA editor (Alt+F11 to open the VB editor) then press Run. Note: if any line is shown in red, it contains an error. smart quotes are the usual cause. Blue, green, blakc text is ok.

You'll also need macro security set on medium or sign the code. See How to use VBA Editor for more information on macro security and signing code.

I tidied the code up a little and added an input box to make it easier - the brackets are added automatically so you just need to enter the file number. This will work on all messages in a search folder. include sent messages.

Sub AddFileNumber()

Dim myolApp As Outlook.Application

Dim calendar As MAPIFolder

Dim aItem As Object

Set myolApp = CreateObject("Outlook.Application")

Set mail = myolApp.ActiveExplorer.CurrentFolder

Dim iItemsUpdated As Integer

Dim strTemp As String

Dim strFilenum As String

strFilenum = InputBox("Enter the filenumber")

iItemsUpdated = 0

For Each aItem In mail.Items
strTemp = "[" & strFilenum & "] " & aItem.Subject
aItem.Subject = strTemp
iItemsUpdated = iItemsUpdated + 1
aItem.Save

Next aItem

MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"

End Sub

Hi Diane,

I snooped in on this message exchange and used your code (above) - works great, except:

I've got hundreds of emails with the subject line starting "Launch Notification: " (no quotes). I'd like to remove this entirely, but keep all that follows.

Are there some lines you could add to your code to accomplish this?

Ideally, I would just type in the text to be deleted in your input box.

I'm Fortran knowledgeable, but VB, Macros and Outlook are all wonders.

Thanks,

John
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You need to use this line:

strTemp = right(aItem.Subject, Len(aItem.Subject)-21)

This counts the length of the subject then subtracts the number of characters you want to drop to determine how many letters to get. It then counts from the right, so the words you want to drop are left behind.

If the folder has a mix of messages and only some will be edited, you need to add an If line (or move the messages to a new folder).

For Each aItem In mail.Items

if left(aitem.subject, 20) = "Launch Notification:" then
aItem.Subject = right(aItem.Subject, Len(aItem.Subject)-21)
iItemsUpdated = iItemsUpdated + 1
aItem.Save

end if
Next aItem

(test it on a couple of messages in a new folder to make sure the count is correct. )
 

John

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Hi Diane,

This is my third attempt at responding to your last message, my previous 2 apparently got dumped in the bit bucket.

Wow! That was a really fast response to my request - Thanks!

I incorporated your patch - works fine.

You've even got me doing a little (veeery little) VBA programming (I had "launch" at the end of some subject lines so swapped the lefts and rights and changed the quote and lengths).

Thanks again. There is one other thing:

I would like to change the message receipt date and time to the launch date and time which is in the body of the message.

I realize this would be a manual one-at-a-time process, but is there a way of doing it at all?

Regards,

John
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Changing the date is more complicated and may be impossible - if it is possible, you need to use Redemption. The information in the message header, including the received date, is normally read-only but some fields can be changed using RDO objects in Redemption.

Getting the date from the body isn't all that difficult if you want to do something with it - but it may not be possible to use it in the Received field.
 

John

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
That's what I thought, just wanted to have it confirmed by an expert.

Thanks agin,

John
 

Lisas4

New Member
Outlook version
Outlook 2007
Email Account
Hi Diane, I love your Script.
I wish you could modify it for me.
When I use your script, it adds some text in breakets before the old content of the subject line.

Could you please eliminate the part that keeps the old content of the subject line, I want only the new number or text to appear in the subject line, and have the original text deleted.

I deleted in this line
strTemp = "[" & strFilenum & "] " & aItem.Subject

the &aItem.Subject

but it still kept working flawless by keeping the original text.

So I am a bit stuck and would be very happy if you could provide me with the modifyed script.

Thank you so much,

Lisa
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Removing that line should remove the subject - this line writes what is in the string:
aItem.Subject = strTemp
and that is the only way to get the subject.

you can use just this too:

aItem.Subject = "[" & strFilenum & "] "
 

TremblayT

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Hi Diane,

Thanks for all the great codes.

I need to insert the received date of the email in the subject. I tried to find the code for it but could not find it. I also need to do it in a loop for all the files I have in a search folder. Right now, I am at the point in my code
Thanks for the help.

Sub AddFileNumberandFolderpath()
Dim myolApp As Outlook.Application
Dim aItem As Object
Dim F As Outlook.MAPIFolder

Set myolApp = CreateObject("Outlook.Application")
Set mail = myolApp.ActiveExplorer.CurrentFolder

Dim iItemsUpdated As Integer
Dim strTemp As String
Dim strFilenum As String

strFilenum = InputBox("Enter the file number")
iItemsUpdated = 0
For Each aItem In mail.Items
Set F = aItem.Parent
Dim strFileDate As String
strTemp = Date & "_" & strFilenum & "_" & aItem.Subject
/* Its probably a very simple object to insert but I could not find it. I tried several variations. Here above I added Date from one of your other code*/
aItem.Subject = strTemp
iItemsUpdated = iItemsUpdated + 1
aItem.Save
Next aItem

MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"

Set myolApp = Nothing

End Sub
 

TremblayT

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Also, is it possible to modify the format of the date to show YYYY/MM/DD?

Thanks,
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
O Batch update calendar Using Outlook 3
M Batch print without appended trail of repeated e Using Outlook 2
F Send multiple batch tasks to single person Using Outlook 3
Diane Poremsky Batch Import Photos into Outlook Contacts Using Outlook 0
Z Batch Change Folder Views in Outlook 2016 Using Outlook 1
N Triggering batch file from Outlook Using Outlook 1
J Hyperlinks to batch files with parameters Using Outlook 3
C Outlook 2007 Removing then adding account restores junk email processing Using Outlook 0
J O365 - Adding Shared Google Calendar ICS link issue in O365 Using Outlook 0
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
S User Defined Fields adding new value (2) Using Outlook 0
M Changing the preferred order for "Put this entry in" list for adding new contacts to the Address Book Using Outlook 1
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
E Project Management - Adding Folders for Different Folder Types Using Outlook.com accounts in Outlook 0
D Adding Enterprise Exchange Email Account to Outlook Prevents Sending via Outlook.com Account Using Outlook.com accounts in Outlook 10
S Adding new Exchange (2016) rule very slow down Microsoft Outlook Exchange Server Administration 0
Z Outlook Custom Form: Adding Dropdown(Project Code) at the end of subject Outlook VBA and Custom Forms 0
Z Adding dropdown list using custom form Outlook VBA and Custom Forms 7
M Adding Macro to populate "to" "subject" "body" not deleting email string below. Outlook VBA and Custom Forms 5
E Unable to open Outlook 2010 after adding new email account Using Outlook 4
O Adding a new account - "CompanyL (none)" line is added Using Outlook 5
broadbander Needing help with reply/reply all while keeping attachments and adding a new CC recipient. Outlook VBA and Custom Forms 5
M adding corresponding contact form data on a mass scale Using Outlook 5
A VB to "reply all" email items stored in a folder of outlook with adding a new message Outlook VBA and Custom Forms 0
K adding more rules to 'different domains check' macro Outlook VBA and Custom Forms 2
P MS OUTLOOK 2013 - Adding Sender on the CC line Using Outlook 5
R User Defined Fields adding new value Using Outlook 3
W Adding A Macro To Message Ribbon Outlook VBA and Custom Forms 2
I Collecting mail address from GAB and adding to Outlook Task Using Outlook 2
A Outlook 2016 - adding outlook.com account creates a new/strange address Using Outlook.com accounts in Outlook 18
F Adding textbox filter to listbox? Outlook VBA and Custom Forms 2
N Recurring invite sent w/distribution list adding/removing attendees Using Outlook 0
J Issues with adding iCloud to Outlook Using Outlook 1
G Adding a contact to Outlook with a custom form using Access VBA Outlook VBA and Custom Forms 1
C Macro to send email after changing from address and adding signature Outlook VBA and Custom Forms 1
J Adding Reply & Delete to main toolbar? Using Outlook 0
T Outlook 2007 adding categories Using Outlook 15
N Adding Appointment Item in Outlook to Shared Calendar Folder Outlook VBA and Custom Forms 7
Diane Poremsky Adding Emojis to Outlook's AutoCorrect Using Outlook 0
T Adding "Mark As Complete" btton to Task Remindet Pop-Up Using Outlook 3
O Saving Attachments to folder on disk and adding Initials to end of file name Outlook VBA and Custom Forms 9
Ascar_CT Adding contacts on Android phone and then syncing them to Outlook Using Outlook.com accounts in Outlook 4
A Adding a 2010 sharepoint contact list to outlook 2010 address book Using Outlook 1
M Adding fields to Task in Outlook Home and Business 2010 Outlook VBA and Custom Forms 7
S Using Send on Behalf is adding extra data in from line Using Outlook 1
Lucylou Outlook 2013 Adding Outlook.com breaks profile, "Outlook not working" messag Using Outlook.com accounts in Outlook 1
C Adding Categories when Composing Email Using Outlook 1
stephenjones Adding a business account to Outlook Using Outlook 1
Chris Grew Adding 2nd Email Address BCM (Business Contact Manager) 3
G Adding an attachment to email I just created (VBA) Outlook VBA and Custom Forms 1

Similar threads

Top