Message search and edit, another way?

Status
Not open for further replies.

jrceloni

New 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.
 
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
 
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.
 
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.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
E Outlook 365 Save Selected Email Message as .msg File - oMail.Delete not working when SEARCH Outlook VBA and Custom Forms 0
J Outlook 2016 After a search in all mailboxes, where is each message that was found? Using Outlook 6
S Create A Search Folder That Looks For Message Class? Outlook VBA and Custom Forms 0
B Search: Cannot find which Folder Contains a Message Using Outlook 3
E Search for folder by key in subject then move new message to related folder Outlook VBA and Custom Forms 1
H Search and delete part of a link inside HTML body message Outlook VBA and Custom Forms 2
H Using Outlook Rules to search for NewLines in message body Using Outlook 1
C Odd error message not found in Google search Using Outlook 1
A Outlook 2007 message search results loses focus Using Outlook 7
Kika Melo How to mark as Junk any message not from Contacts (in Outlook.com) Using Outlook 3
G Outlook 365 My iCloud Outlook doesn’t work after reinstalling Microsoft365 on Windows 10 PC – now I get error message on contacts and calendar Using Outlook 1
G Get current open draft message body from VBA Outlook VBA and Custom Forms 1
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
F Outlook 2019 Forwarding Message and Keeping Unread Status Outlook VBA and Custom Forms 0
E Save Selected Email Message as .msg File - digitally sign email doesn't works Outlook VBA and Custom Forms 1
C Populate form data into message body Outlook VBA and Custom Forms 1
J Outlook 365 Add keywords from Listbox to the message body where cursor is placed Outlook VBA and Custom Forms 0
B How to create a button that sorts and selects the most recent message with ONE click Using Outlook 2
P "Item could not be moved" message occurs frequently for IMAP inbox, Office 365 Using Outlook 0
A rule name into message - how? Outlook VBA and Custom Forms 5
A Links in email getting error message about group policy Using Outlook 4
O How to find and replace a word in Outlook-Agenda-Subject and Message? Using Outlook 0
X Open Hyperlinks in an Outlook Email Message (Help with Diane's solution) Outlook VBA and Custom Forms 3
Commodore Unable to move message Using Outlook 3
Y Disable Microsoft Outlook Test Message Using Outlook 4
S Unable to change Message Class Outlook VBA and Custom Forms 0
S Changing Message Class Outlook VBA and Custom Forms 4
Cathy Rhone Mail merge error message Using Outlook 1
C Can't Locate an Unread Message in my Outlook view pane Using Outlook 0
G Send a greeting message to a contact on birthday Outlook VBA and Custom Forms 5
B Inconsistent handling of message read/unread status by Outlook Using Outlook 3
N Save Selected Email Message as .msg File Outlook VBA and Custom Forms 12
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
O Outlook 2010 Add delete button to the side of the message list Using Outlook 1
Cathy Rhone The properties of the message have been changed Using Outlook 1
S Outlook 2016 A Shortcut to recall and delete and sent message. Using Outlook 1
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
S Unable to extract text from an Outlook email message Using Outlook 2
R Outlook 2016 Message-ID oddity/inconsistency? Using Outlook 7
J Implement Keywords based on body message Outlook VBA and Custom Forms 0
C must select message to trigger safe list Using Outlook 3
Witzker HowTo Change message Class of contact form Outlook VBA and Custom Forms 0
D.Moore VB script to Digitaly Sign newly created outlook message Outlook VBA and Custom Forms 2
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
W September 2020 - No Default Email Client message after Office Update Using Outlook 1
A Flag Message for Follow Up after sending Outlook VBA and Custom Forms 1
S Outlook (2016 32bit; Gmail IMAP) - Save sent message to Outllook Folder Outlook VBA and Custom Forms 0
C Why won't Title display in message list? Using Outlook 1
icacream content in this message could not be downloaded.... Using Outlook 2
R Warn before sending message Outlook VBA and Custom Forms 4

Similar threads

Back
Top