PDA

View Full Version : Solved: Changing subject of selected/highlighted emails



stevietv
02-09-2009, 03:04 AM
Hi Guys

I'm trying to write a macro to rename multiple emails that have been highlighted in the folder view.

The below code is what I've come up with so far, although it only seems to rename the first email. The code pops up an alert box to ask what you would like the emails to be renamed to.

The wierd thing is that if I try to change another setting like msn.unread = true then it works for all the emails. Also adding an alert to show which email is the current email in the loop also shows that it is stepping through each email.

Can anyone advise what I need to change in order to set the subject for each email selected.


Sub renameEmails()
On Error Resume Next
Dim MsgColl As Object
Dim msg As Outlook.MailItem
Dim objNS As Outlook.NameSpace
Dim i As Long
Dim subjectname As String

Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
' a collection of selected items
Set MsgColl = ActiveExplorer.Selection
Case "Inspector"
' only one item was selected
Set msg = ActiveInspector.CurrentItem
End Select
On Error GoTo 0

If (MsgColl Is Nothing) And (msg Is Nothing) Then
GoTo ExitProc
End If
subjectname = InputBox("Enter Subject Name", "Subject?")
If Not MsgColl Is Nothing Then
For i = 1 To MsgColl.Count
' set an obj reference to each mail item so we can move it
Set msg = MsgColl.Item(i)
With msg
.subject = subjectname
End With
Next i
ElseIf Not msg Is Nothing Then
msg.subject = subjectname
End If
ExitProc:
Set msg = Nothing
Set MsgColl = Nothing
Set olMyFldr = Nothing
Set objNS = Nothing
End Sub

JP2112
02-09-2009, 08:42 PM
When you change the subject, you have to call the Save Method to actually update it.

Change

With msg
.subject = subjectname
End With

to

With msg
.subject = subjectname
.Save
End With

HTH



Hi Guys

I'm trying to write a macro to rename multiple emails that have been highlighted in the folder view.

The below code is what I've come up with so far, although it only seems to rename the first email. The code pops up an alert box to ask what you would like the emails to be renamed to.

The wierd thing is that if I try to change another setting like msn.unread = true then it works for all the emails. Also adding an alert to show which email is the current email in the loop also shows that it is stepping through each email.

Can anyone advise what I need to change in order to set the subject for each email selected.


Sub renameEmails()
On Error Resume Next
Dim MsgColl As Object
Dim msg As Outlook.MailItem
Dim objNS As Outlook.NameSpace
Dim i As Long
Dim subjectname As String

Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
' a collection of selected items
Set MsgColl = ActiveExplorer.Selection
Case "Inspector"
' only one item was selected
Set msg = ActiveInspector.CurrentItem
End Select
On Error GoTo 0

If (MsgColl Is Nothing) And (msg Is Nothing) Then
GoTo ExitProc
End If
subjectname = InputBox("Enter Subject Name", "Subject?")
If Not MsgColl Is Nothing Then
For i = 1 To MsgColl.Count
' set an obj reference to each mail item so we can move it
Set msg = MsgColl.Item(i)
With msg
.subject = subjectname
End With
Next i
ElseIf Not msg Is Nothing Then
msg.subject = subjectname
End If
ExitProc:
Set msg = Nothing
Set MsgColl = Nothing
Set olMyFldr = Nothing
Set objNS = Nothing
End Sub

stevietv
02-10-2009, 02:57 AM
thanks for that - solved everything!

JP2112
02-10-2009, 05:34 AM
Glad to hear it worked.

--JP


thanks for that - solved everything!

Xandra
10-15-2014, 02:51 AM
So my vision is, an email is received in my inbox folder on Outlook. The email will always have a month in the subject field written in full (e.g. January, February etc). Subfolders will be contained within the inbox folder labelled as the months of the year. The macro i have tried to form will, when run, search the inbox folder and transfer all the emails with a subject field containing "January" into the subfolder "January", all the emails with a subject field containing "February" into the subfolder "February"...and so on for the remaining 10 months of the year.

westconn1
10-15-2014, 01:38 PM
i posted this in another thread a few days ago


Function getmonth(subject) As String
For mnth = 1 To 12
mnthname = Format(DateSerial(Year(Now), mnth, 1), "mmmm")
If InStr(1, subject, mnthname, vbTextCompare) > 0 Then getmonth = mnthname: Exit Function
Next
End Function

' call like

folder = getmonth(msg.subject)
you can then use the returned month name from the subject to move the email to the correct folder or add a folder if it does not already exist