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.
 

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

New 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.
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
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
B MS Office 2010-calendar search feature; whenever I invoke this, I get an error message and the s/w closes MS Office and restart it Using Outlook 2
R Reading email problems. Bing search box appears in the message area Using Outlook 1
Y A "Cannot open this item" error message occurs when you try to open a message from the search result Using Outlook 9
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
T Column to display which email alias a message was sent to Outlook VBA and Custom Forms 6
V Change start time based on message duration Outlook VBA and Custom Forms 2
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
T Missing text in e-mail message after "(Please keep confidential)" Using Outlook 3
C Custom Form (seperate layout pages and message reading pane) Outlook VBA and Custom Forms 0
C Create new Message with shared contacts & BCC'ing recipients Outlook VBA and Custom Forms 0
M Message list font changed after update Using Outlook 2
BretAB Is it possible to add a lookup field to a Message form? Outlook VBA and Custom Forms 4
D Unopened message in inbox deleted and not in deleted items Using Outlook 3
T Change the selected Message in the Outlook window Outlook VBA and Custom Forms 2
geofferyh Cannot get Macro to SAVE more than one message attachment??? Outlook VBA and Custom Forms 5
W Message class changes of a custom form changes to the default form Using Outlook 2
evdbogaard Reply with only last message cited Using Outlook 1
T O365 and Office Message Encryption Exchange Server Administration 2
S Custom Form, copy user field data to message body Outlook VBA and Custom Forms 12
D Outlook Message Window Changes Size and Position Using Outlook 1
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
R Retain Original Message When Forwarding With Macro Outlook VBA and Custom Forms 3
R Call a Public Sub when a Flag is clicked on in the Message Preview pane Outlook VBA and Custom Forms 1
C Macro to add multiple recipients to message Outlook VBA and Custom Forms 3
P Message List Font Changed Using Outlook 6
I Automating message move between folders Outlook VBA and Custom Forms 0
U Outbox Message Stuck after reading some MailItem Properties with VBA Outlook VBA and Custom Forms 1
e_a_g_l_e_p_i Is there a way to add something that is in the "Format Text" tab to the "basic Text" on the message tab Using Outlook 1
R VBA macro - new message Outlook VBA and Custom Forms 3
I How to display sender's name instead of email address in outlook 2013 message Using Outlook 5
J Outlook 2016 message content does not display - outlook.com; exchange Using Outlook.com accounts in Outlook 9
D Paste Excel table into Outlook message Outlook VBA and Custom Forms 6
W Message Text Using Outlook 1
P Unread Until Click Another Message? Using Outlook 2
N Saving And Deleting Outlook Attachments with Unknown Error Message Outlook VBA and Custom Forms 1
J Searching message folder in Outlook 2016 Using Outlook 5
Similar threads


















































Top