none
Outlook VBA: Altering Code for Numbering end of subject line sequentially RRS feed

  • Question

  • Hello,

    For Microsoft Office Outlook 2013 (desktop version, not online)

    Someone from Microsoft at the Microsoft Answers Community forum directed me here (I'm not sure if I selected the correct forum). I would appreciate some help. 

    I've put together 2 codes from the internet to add sequential numbers to the end of the subject line on emails I select. I will have several emails with the same subject line and I want to add sequential number to the end of the subject line b/c I some of the emails to the network folder. I got tired of manually having to rename the emails when I save them to the network at work.

    This works below, but:

    1) I would like to have the numbering to start over the next time I select a group of emails, if possible. Currently, 

    Ex: subject line of 1st email_1

    subject line of 2nd email_2

    but next time I run code and select emails it continues with 3 and I would like to start at 1 again, if possible.

    2) I would like it to number the oldest or first email with 1 and the newest email in the string of emails with a later number. As it is right now, the code will number the 1st email re: a certain subject with later number and the newest email with 1.

    I would appreciate help with both questions, but if this is too hard, the 2nd question is the most needed.

    Thank you for any help.

    Sub NumberSubjectsSelectedMail()
    'numbers subject line for selected mail - this code is just the 'select mail' part of the code
    'https://stackoverflow.com/questions/25315291/in-outlook-is-it-possible-to-run-a-macro-on-an-email-i-select-manually
        Dim x, mailItem As Outlook.mailItem
        For Each x In Application.ActiveExplorer.Selection
            If TypeName(x) = "MailItem" Then
                Set mailItem = x
                Call fooMail(mailItem)
            End If
        Next
    End Sub
    
    Sub fooMail(ByRef mItem As Outlook.mailItem)
    'code that numbers subject line
    'https://www.slipstick.com/outlook/email/add-a-file-number-or-keyword-to-the-subject-line-of-messages/
     Dim sAppName As String
     Dim sSection As String
     Dim sKey As String
     Dim lRegValue As Long
     Dim lFormValue As Long
     Dim iDefault As Integer
     
     sAppName = "Outlook"
     sSection = "Messages"
     sKey = "Current Number"
    ' The default starting number.
         iDefault = 1 ' adjust as needed
     
     ' Get stored registry value, if any.
         lRegValue = GetSetting(sAppName, sSection, sKey, iDefault)
     
     ' If the result is 0, set to default value.
         If lRegValue = 0 Then lRegValue = iDefault
     
     ' Increment and update invoice number.
         SaveSetting sAppName, sSection, sKey, lRegValue + 1
         
     'changed code from website from Item to mItem to match variables in above code.
     mItem.Subject = mItem.Subject & "_" & CStr(lRegValue)
     mItem.Save
         
    End Sub


    • Edited by 123Group456 Monday, October 9, 2017 11:54 PM Gave Outlook version number
    Monday, October 9, 2017 11:52 PM

Answers

  • Hello,

    You could use:

    Sub NumberSubjectsSelectedMail()
        Dim x, mailItem As Outlook.mailItem
        Dim num As Integer
        For Each x In Application.ActiveExplorer.Selection
            If TypeName(x) = "MailItem" Then
                Set mailItem = x
                num = num + 1
                mailItem.Subject = mailItem.Subject & "_" & num
                mailItem.Save
            End If
        Next
    End Sub

    For your 2nd question, you could sort the item when you select. If sorting doesn't work, please share how you get or select these items.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by 123Group456 Tuesday, October 10, 2017 6:20 PM
    Tuesday, October 10, 2017 5:54 AM
    Moderator

All replies

  • Hello,

    You could use:

    Sub NumberSubjectsSelectedMail()
        Dim x, mailItem As Outlook.mailItem
        Dim num As Integer
        For Each x In Application.ActiveExplorer.Selection
            If TypeName(x) = "MailItem" Then
                Set mailItem = x
                num = num + 1
                mailItem.Subject = mailItem.Subject & "_" & num
                mailItem.Save
            End If
        Next
    End Sub

    For your 2nd question, you could sort the item when you select. If sorting doesn't work, please share how you get or select these items.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by 123Group456 Tuesday, October 10, 2017 6:20 PM
    Tuesday, October 10, 2017 5:54 AM
    Moderator
  • Thank you very much, Celeste. You answered both questions--sorting solved the 2nd question.

    God bless you.

    • Edited by 123Group456 Tuesday, October 10, 2017 6:21 PM
    Tuesday, October 10, 2017 6:21 PM