Message search and edit, another way?

jrceloni

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
While composing a reply, I want to do simple repetitive editing of the message body: e.g. when cursor is at the '<' an e-mail address, e.g. "Wig, Big <bigwig@contoso.com>", I want to hit a hotkey and strip the address (leaving "Wig, Big"; bonus if it moves the cursor to the next '<'). Long ago I did a little VBA in Word, Outlook, and Access but don't see an easy way to do this. (I don't even need to do a loop; in fact I don't want to since I don't necessarily want to strip all the <...> strings.)

I now have an AutoHotKey hotkey to do Del, Ctrl-Del, Del, then alt-o f d f space alt-f to find the next one, but it only works when the address inside the <> is really a link, since if it's just text, it'll just delete the first "word" of the address. A VBA solution could work more reliably and allow more flexibility...

Thank you.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This is from one i use in Word to remove 0 from leading dates, with the pattern changed to remove < address > and it tweaked to work in outlook. it may be a bit messy...

select the address - you actually only need to select the <address> part, but could just do a select all instead - the pattern looks for an email address inside <>

.Pattern = "(<(.*)@(.*)>)"

2818

Code:
Sub RemoveAddress()

Dim objItem As Object
Dim objInsp As Outlook.Inspector
  
' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
'On Error Resume Next


Dim SectionText As String
Dim RegEx As RegExp, Matches As Object, Match As Object
Dim i As Integer
Dim M1 As MatchCollection
Dim M As Match

Set RegEx = CreateObject("vbscript.regexp")
With RegEx
    .Global = True
    .MultiLine = False
    .Pattern = "(<(.*)@(.*)>)"
End With
  
'Reference the current Outlook item
Set objItem = Application.ActiveInspector.currentItem
If Not objItem Is Nothing Then
  If objItem.Class = olMail Then
    Set objInsp = objItem.GetInspector
    If objInsp.EditorType = olEditorWord Then
      Set objDoc = objInsp.WordEditor
      Set objWord = objDoc.Application
      Set objSel = objWord.Selection

' replace the With block with your code
With objSel
SelectionText = objSel.Text
  If RegEx.Test(SelectionText) Then
      Set M1 = RegEx.Execute(SelectionText)

For Each M In M1
 strFind = M.SubMatches(0)
  
  objSel.Find.ClearFormatting
  objSel.Find.Replacement.ClearFormatting
  With objSel.Find
    .Text = strFind
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  objSel.Find.Execute Replace:=wdReplaceAll

Next

  Set MsgMatches = Nothing
  Set Match = Nothing
   End If
          
End With

      End If
  End If
End If
    Set RegEx = Nothing
    Set objItem = Nothing
    Set objWord = Nothing
    Set objSel = Nothing
    Set objInsp = Nothing
End Sub
 

jrceloni

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Wow that was fast; thank you.

I had limited success and also see that I know much less than I thought. I did create a module and via Tools/Refs added the Word and RegExp libraries (Word alone didn't seem enough), then opened an Outlook mail item, manually selected a <email@addre.ss>, ran the sub via VB menu, and the <...> went away, but when I just put the cursor at or before the '<' or selected more or less text around the <...> or selected all, either it didn't work or generated an error. Then (optimistic it would eventually work) I navigated around looking for a place to assign the sub to a hotkey (as I would a macro in Word) and couldn't find it.

Since I'd like to understand enough to make it work, I'll shift my question to what resource(s) you recommend to an old programmer (1st languages were Fortran and 360 assembly language, made it through Basics and C's etc. but now do more with SQL...) to get a little better at VBA...

Thanks again.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
fast is easy when i already had the macro. :)
I just put the cursor at or before the '<' or selected more or less text around the <...> or selected all, either it didn't work or generated an error.
that is because it's working on the selection -
Set objSel = objWord.Selection

Select a paragraph or the entire email and run it.

Outlook doesn't do shortcuts - you can add it a button for it to Quick access toolbar or to the ribbon.
 
Top