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
Bookmarks