Consulting

Results 1 to 6 of 6

Thread: Solved: Changing subject of selected/highlighted emails

  1. #1

    Solved: Changing subject of selected/highlighted emails

    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.

    [vba]
    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
    [/vba]

  2. #2
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    When you change the subject, you have to call the Save Method to actually update it.

    Change

    [VBA]With msg
    .subject = subjectname
    End With[/VBA]

    to

    [VBA]With msg
    .subject = subjectname
    .Save
    End With[/VBA]

    HTH


    Quote Originally Posted by stevietv
    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.

    [vba]
    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
    [/vba]

  3. #3
    thanks for that - solved everything!

  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    Glad to hear it worked.

    --JP

    Quote Originally Posted by stevietv
    thanks for that - solved everything!

  5. #5
    VBAX Newbie
    Joined
    Oct 2014
    Posts
    1
    Location
    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.
    Get 700-038 questions demos for test king and SAP with 100% success guaranteed. Our high quality University of California, Los Angeles itil prepares you well before appearing in the final exams of Alliant International University gmat.

  6. #6
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •