+ Reply to Thread
Results 1 to 6 of 6

Replace entire subject line

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,083

    Replace entire subject line

    I have a macro I made a few years ago, which replaces a value in the subject lines of all emails you've selected. The code is below. I ran into a snag this morning, and am at a loss as to how to work around it. My macro was already working if I want to replace a value with nothing; in that case, for "nothing" I just leave the field blank. But today I ran into a situation where I wanted to replace the full subject line with a different value. Putting an Asterisk in the Text to Replace causes the macro to replace nothing, and putting nothing in the Text to Replace also causes no replacement. How would I tell me macro to replace ALL of the subject line?
    Sub ReplaceInSubjectline()
    'Inspiration http://www.excelforum.com/outlook-programming-vba-macros/1114327-change-each-subject-line-in-selection.html?
    'Replaces anything in Subject Line with anything or nothing
    Dim mySubject As String
    Dim MyOlSelection As Object
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
    Dim X As Long
    Dim RepWhat As String
    Dim RepWith As String
    Dim iReply As Integer
        
        RepWhat = InputBox(Prompt:="What text do you want to replace?", _
        Title:="ENTER YOUR TEXT TO REPLACE", Default:="Type String to Replace")
        If RepWhat = "Type String to Replace" Then Exit Sub
        If RepWhat = vbNullString Then
            iReply = MsgBox(Prompt:="You didn't put a value to replace." & vbCrLf & _
            "To replace the entire subject line, Click YES" & vbCrLf & _
            "To exit this macro, click NO", _
            Buttons:=vbYesNo + vbQuestion, Title:="REPLACE ENTIRE SUBJECT LINE?")
            If iReply = vbNo Then Exit Sub
        End If
            
        RepWith = InputBox(Prompt:="OK, we're replacing " & RepWhat & ".  " & vbcrlof & _
        "What text do you want to replace it with?", _
        Title:="ENTER YOUR REPLACEMENT TEXT", Default:="Type what you want to appear")
        If RepWith = "Type what you want to appear" Then Exit Sub
        If RepWith = vbNullString Then
            iReply = MsgBox(Prompt:="You didn't put a replacement text." & vbCrLf & _
            "To replace with Nothing, Click YES" & vbCrLf & _
            "To exit this macro, click NO", _
            Buttons:=vbYesNo + vbQuestion, Title:="REPLACE WITH NOTHING?")
            If iReply = vbNo Then Exit Sub
        End If
            
        Set myOlExp = Application.ActiveExplorer
        Set myOlSel = myOlExp.Selection    'The big selection
        
        For X = 1 To myOlSel.Count
            Set MyOlSelection = Application.ActiveExplorer.Selection(X)  ' Select email
            mySubject = Replace(MyOlSelection.Subject, RepWhat, RepWith)
            MyOlSelection.Subject = mySubject
            MyOlSelection.Save
        Next X
        
    ExitRoutine:
        Set myOlExp = Nothing
        Set myOlSel = Nothing
        Set MyOlSelection = Nothing
        
    End Sub
    I know I'm not stupid, but I suspect I'm a lot stupider than I think I am

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Replace entire subject line

    As far as I know VBA Replace doesn't work with wildcards, so if you've put '*' as the value you want to replace then replace is going to try and find the literal string '*'.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,083

    Re: Replace entire subject line

    I agree, but knowing that still doesn't help with "How would I tell me macro to replace ALL of the subject line? "

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Replace entire subject line

    Check if RepWhat is '*' and if it is replace the entire subject line with RepWith.
    If RepWhat = "*" Then
        mySubject = RepWith
    End If
    Last edited by Norie; 10-16-2019 at 01:45 PM.

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,083

    Re: Replace entire subject line

    Good solution! I can work with that one. Thanks!

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,083

    Re: Replace entire subject line

    Whoops, almost forgot to post my updated macro. I try to make it habit to post the final solution (or at least MY final solution) in case someone else needs it.
    Sub ReplaceInSubjectline()
    'Inspiration http://www.excelforum.com/outlook-programming-vba-macros/1114327-change-each-subject-line-in-selection.html?
    'Replaces anything in Subject Line with anything or nothing
    Dim mySubject As String
    Dim MyOlSelection As Object
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
    Dim X As Long
    Dim RepWhat As String
    Dim RepWith As String
    Dim iReply As Integer
        
        RepWhat = InputBox(Prompt:="What text do you want to replace?", _
        Title:="ENTER YOUR TEXT TO REPLACE", Default:="Type String to Replace")
        If RepWhat = "Type String to Replace" Or RepWhat = vbNullString Then Exit Sub
        If RepWhat = "*" Then
            RepWhat = "the Entire Subject Line"
            
        RepWith = InputBox(Prompt:="OK, we're replacing " & RepWhat & ".  " & vbcrlof & _
        "What text do you want to replace it with?", _
        Title:="ENTER YOUR REPLACEMENT TEXT", Default:="Type what you want to appear")
        If RepWith = "Type what you want to appear" Then Exit Sub
        If RepWith = vbNullString Then
            iReply = MsgBox(Prompt:="You didn't put a replacement text." & vbCrLf & _
            "To replace with Nothing, Click YES" & vbCrLf & _
            "To exit this macro, click NO", _
            Buttons:=vbYesNo + vbQuestion, Title:="REPLACE WITH NOTHING?")
            If iReply = vbNo Then Exit Sub
        End If
            
        Set myOlExp = Application.ActiveExplorer
        Set myOlSel = myOlExp.Selection    'The big selection
        
        For X = 1 To myOlSel.Count
            Set MyOlSelection = Application.ActiveExplorer.Selection(X)  ' Select email
            If RepWhat = "the Entire Subject Line" Then
                mySubject = RepWith
            Else
            mySubject = Replace(MyOlSelection.Subject, RepWhat, RepWith)
            End If
            MyOlSelection.Subject = mySubject
            MyOlSelection.Save
        Next X
        
    ExitRoutine:
        Set myOlExp = Nothing
        Set myOlSel = Nothing
        Set MyOlSelection = Nothing
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help comparing two files by subject+Date, subject+DatePlus1, Subject+DateMinus1
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 07-11-2017, 07:37 AM
  2. [SOLVED] Subject line: Pivot chart’s one line is correct but the other line not correct?
    By Mirisage in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 10-21-2014, 11:20 AM
  3. How to highlight the ENTIRE row subject to a specific cell?
    By sami770 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-06-2012, 03:37 AM
  4. Replies: 1
    Last Post: 05-11-2011, 07:11 AM
  5. Customize Subject Line
    By terezakat in forum Excel General
    Replies: 0
    Last Post: 07-25-2005, 05:47 PM
  6. Using a cell value as the subject line
    By Qaspec in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2005, 11:05 AM
  7. [SOLVED] Populate the subject line.
    By Scott Hutchinson in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-18-2005, 05:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1