Need Help with Simple Code Correction

Status
Not open for further replies.

Coco

Member
Outlook version
Email Account
Exchange Server
I'm very new to VB (like, several hours old!), and I have created a small code to run on an email that will add the email's original date stamp to the beginning of the subject line. The problem I am having (and I'm sure it is a simple code correction) is that it only works on one email at a time (the one selected). I need to be able to select a bunch of emails and have it run on all of them. Here is my code:

Sub Test()
Dim MItem As MailItem
On Error GoTo ExitPoint
For Each MItem In ActiveExplorer.Selection
MItem.Subject = MItem.CreationTime & " " & MItem.Subject
Next

ExitPoint:

End Sub

Anyone who can help I would be much appreciative!

Thanks
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Try something like this - i didn't test it and if it doesn't quite work, check the macro here - it 'does something' to selected items.

Sub Test()
Dim currentExplorer As Explorer
Dim Selection As Selection
Dim obj As Object
Dim MItem As MailItem
Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection
On Error GoTo ExitPoint
For Each obj In Selection
Set MItem = obj
MItem.Subject = MItem.CreationTime & " " & MItem.Subject

mitem.save
Next
ExitPoint:
End Sub
 

Coco

Member
Outlook version
Email Account
Exchange Server
Thank you Diane,

I see the problem is that it is looking at currently viewed messages (as in, messages in the message pane). And only one message can be in the view pane at a time. If I simply highlight a message and am not viewing it in the pane, the code will not work at all. Since only one message could be possibly viewed at a time in the open pane, it still only works on one message at a time. I think I need a code that would work on all messages in a folder or something?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
My code should work on the selection - it doesn't matter if it's being 'viewed'. just only need to select it. I'm working on an ediscovery project right now and can't double check the code until I'm finished with this though.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
One thing you need to be aware of - 'created date' is generally going to be the date the message was either added to your mailbox or when it was downloaded. It's better to use Sent or Received date.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This worked here -

Code:
Public Sub ChangeSubject()
   Dim currentExplorer As Explorer
   Dim Selection As Selection
   Dim currentItem As Object
   Dim mItem As MailItem
   Dim obj As Object
   Set currentExplorer = Application.ActiveExplorer
   Set Selection = currentExplorer.Selection
   On Error Resume Next
   For Each obj In Selection
           Set mItem = obj
           With mItem
               .Subject = "5: " & .Subject
               .Save
           End With
       Err.Clear
   Next
   Set currentExplorer = Nothing
   Set obj = Nothing
   Set Selection = Nothing
   Set currentItem = Nothing 
 
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
ah... i see what we missed:

mitem.save before Next. Your code works with the save added too.
 

Coco

Member
Outlook version
Email Account
Exchange Server
What command line would I use instead of CreationDate to pull either date sent/received?
 

Coco

Member
Outlook version
Email Account
Exchange Server
Ok, macro up and running nicely, but I need to adjust. If the macro is inadvertently run a second time on the same email, the sent on date is inserted again, resulting in a subject line that reads: sentOn date + (sentOn date + original subject) .

Can I add some type of If/then statement that will detect if the subject already contains the sentOn date (the macro was already run on it) and skip that email?

Thanks,

Coco
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
that's why my sample used "5: " - it fits better after a few tests :)

Add this

For Each obj In Selection
Set mItem = obj
If Left(mItem.Subject, 6) <> Left(mItem.CreationTime, 6) Then

' more
Else

End If
Next
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
Marc2019 Need help please! Cannot Setup my outlook email account on my Mac Outlook 2011 Using Outlook.com accounts in Outlook 2
C need help setting up outlook first time Using Outlook 1
N Need help syncing contacts to iPhone X Using Outlook 8
S Error using AddressEntry.GetContact - need help Outlook VBA and Custom Forms 2
EaglePI Outlook 2010 need help with rules Using Outlook 0
B Need Help - Willing to pay Outlook VBA and Custom Forms 10
J Need Help with Contacts VBA Outlook VBA and Custom Forms 1
H Need help setting up GetFolderPath-Makro with Vodafone IMAP Mail-Account Outlook VBA and Custom Forms 0
T Need help with finding/updating task Outlook VBA and Custom Forms 1
Denis Hi everyone!!! Need some help with Excel to Exchange Calendar??PLEASE>>> Using Outlook 1
E Need Help on Setting up a repeated Reminder on Task with Reoccurence Every Year Using Outlook 6
B Need Help Creating Email Based on Subject w Address in Body Outlook VBA and Custom Forms 1
G Need help configuring a PRF for Outlook 2010 and 2013 Using Outlook 0
K Excel Import Option: Need Help Updating Existing Records BCM (Business Contact Manager) 0
K Need help concerning emails linking to BCM. BCM (Business Contact Manager) 0
J Need help with installation of BCM in Outlook2013 BCM (Business Contact Manager) 5
F Outlook 2010 - Need help with a custom field. Using Outlook 3
D Need Help with Script. Keep getting Runtime Error 438 BCM (Business Contact Manager) 4
R Need help getting BCM to work with my Outlook 2014 Using Outlook 4
L Need help getting calendar/contacts/tasks/notes back! Using Outlook 3
R [VBA] complicated(?) outlook events - need help with code Using Outlook 15
P Need help/Gmail accounts show separately/Outlook 2013 Using Outlook 0
J Need Help Extracting Email Content into Excel Specifically the Attachment Name Using Outlook 3
A Need Help with BCM2010 Sharing on outlook 2013 Scenario! BCM (Business Contact Manager) 1
W I Need help with vb.net and outlook 2010 Using Outlook 2
A My move to MS is not going too well: Need Help !!! Using Outlook 14
M Need help with combo boxes in messages! Using Outlook 10
D Need help understanding which pst-file to use! Using Outlook 0
U Need a help please Outlook 2007 crashing Using Outlook 2
Z Need Help Outlook 2007 inbox and address book view Using Outlook 2
B Need help adding a background color to the compact list items in Month view. Calendar Printing Assistant 1
B Need help adding an image next to the Month Name in the header. Calendar Printing Assistant 1
B Ok - a complicated one - but really need help (auto formatting) Using Outlook 9
B OUTLOOK 2000, Need Help ! Outlook Wishlist 1
P To Use BCM or not...I need help BCM (Business Contact Manager) 1
T Need help making RULE to prepend email SUBJECT Using Outlook 3
V Need help on configuring Outook 2003 Rules and Alerts... Using Outlook 2
A Need Help Urgent !!!- Add Seconde Node To The DAG Exchange Server Administration 12
M Need help w/ Calendar Events not appearing in Outlook 2010 Using Outlook 1
D Need help bulk exporting mailboxes Exchange Server Administration 3
N Need Help with Outlook 2010 Using Outlook 1
G Need help in merging data from multiple .pst data files into one .pst file. Using Outlook 3
F Incoming email titles - i need them back (please help...) Using Outlook 2
B Need help with contact recovery, since trial of Office 2010 has ended. Using Outlook 2
M Need Help on Designing Exchange 2010 Email Solution for 4000 Users Exchange Server Administration 1
S OL2007/XP views will not retain settings. Really need some help here. Using Outlook 2
D Need help repairing Outlook 2010. Using Outlook 2
D Need help with word wrap in Outlook 2010. Using Outlook 5
L Windows Instant Search Indexing not working need help reading Event Log Using Outlook 2
N need help retrieving email from previous Outlook Profile Using Outlook 7

Similar threads

Top