PDA

View Full Version : Batch 'Insert Subject Prefix' macro



lamensterms
06-05-2015, 12:42 AM
Hi guys,

Just wondering if I could please have some assistance tweaking a bit of code I found here... http://www.vbaexpress.com/forum/showthread.php?52447-Change-Subject-on-Outlook-Mail-and-Forward
The intent is to be able to select a bunch of emails (in inbox) and insert a prefix into the subject line.


Sub Test()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.Item(1)

Dim Subject As String
Dim Subjectn As String

Subject = olMsg.Subject
Subjectn = "3322 - " + Subject
olMsg.Subject = Subjectn

lbl_Exit:
Set olMsg = Nothing
Exit Sub
End Sub

... is what I've got so far. It works, but only on 1 of the selected emails. Set olMsg = ActiveExplorer.Selection.Item(1) is the line that I think I need to change, but I'm afraid I don't know what to change it to (or if there is a better way to achieve my goal?).

Thanks a lot for any help.

gmayor
06-05-2015, 01:44 AM
Frankly I am not sure why you would want to do this, but ...



Sub Test()
Dim olMsg As MailItem
On Error GoTo lbl_Exit
For Each olMsg In Application.ActiveExplorer.Selection
If olMsg.Class = OlObjectClass.olMail Then
olMsg.Subject = "3322 - " & olMsg.Subject
olMsg.Save
End If
Next olMsg
lbl_Exit:
Set olMsg = Nothing
Exit Sub
End Sub

lamensterms
06-08-2015, 07:30 PM
Thanks so much for that gmayor. Final code for anyone interested:


Sub Test()
Dim olMsg As MailItem
On Error GoTo lbl_Exit

Dim strFilenum As Variant
strFilenum = InputBox("Enter email prefix")

For Each olMsg In Application.ActiveExplorer.Selection
If olMsg.Class = OlObjectClass.olMail Then
olMsg.Subject = strFilenum & " - " & olMsg.Subject
olMsg.Save
End If
Next olMsg
lbl_Exit:
Set olMsg = Nothing
Exit Sub
End Sub

This linked helped too...

http://www.slipstick.com/outlook/email/add-a-file-number-or-keyword-to-the-subject-line-of-messages/