Consulting

Results 1 to 3 of 3

Thread: Batch 'Insert Subject Prefix' macro

  1. #1
    VBAX Regular lamensterms's Avatar
    Joined
    Apr 2014
    Location
    Pakenham
    Posts
    16
    Location

    Batch 'Insert Subject Prefix' macro

    Hi guys,

    Just wondering if I could please have some assistance tweaking a bit of code I found here... http://www.vbaexpress.com/forum/show...il-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.

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular lamensterms's Avatar
    Joined
    Apr 2014
    Location
    Pakenham
    Posts
    16
    Location
    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/ema...e-of-messages/
    Last edited by lamensterms; 06-09-2015 at 12:09 AM.

Posting Permissions

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