Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-26-2018, 07:37 AM
TomDW TomDW is offline Macro to remove underscores, except in email address Windows 10 Macro to remove underscores, except in email address Office 2016
Novice
Macro to remove underscores, except in email address
 
Join Date: Sep 2018
Posts: 3
TomDW is on a distinguished road
Default Macro to remove underscores, except in email address

Hi,

I'm trying to replace all underscores in a word document with a single space, except for underscores in email addresses.
Could someone please help?
I've got the following already:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "_"
.Replacement.Text = " "
.Forward = True


.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


This will replace all underscores. I don't know how to ignore email addresses...
Thanks in advance!
Reply With Quote
  #2  
Old 09-26-2018, 04:20 PM
macropod's Avatar
macropod macropod is offline Macro to remove underscores, except in email address Windows 7 64bit Macro to remove underscores, except in email address Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Are your email addresses formatted as hyperlinks?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-27-2018, 12:05 AM
TomDW TomDW is offline Macro to remove underscores, except in email address Windows 10 Macro to remove underscores, except in email address Office 2016
Novice
Macro to remove underscores, except in email address
 
Join Date: Sep 2018
Posts: 3
TomDW is on a distinguished road
Default

Hi Macropod,

Unfortunately the e-mail addresses are not formatted as hyperlinks.
The Word document is the result of merging fields, and apparently this doesn't automatically format the e-mail addresses...
Reply With Quote
  #4  
Old 09-27-2018, 12:35 AM
gmayor's Avatar
gmayor gmayor is offline Macro to remove underscores, except in email address Windows 10 Macro to remove underscores, except in email address Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

In that case maybe
Code:
Sub Macro1()
Dim oRng As Range
Dim oWord As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:="_")
            Set oWord = oRng.Duplicate
            If oWord.Words(1).Start <> ActiveDocument.Range.Start Then
                oWord.MoveStartUntil Chr(32), wdBackward
                oWord.MoveEndUntil Chr(32) & Chr(13)
                If InStr(1, oWord.Text, "@") = 0 Then oRng.Text = " "
            End If
            oRng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Set oWord = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 09-27-2018, 12:41 AM
Guessed's Avatar
Guessed Guessed is offline Macro to remove underscores, except in email address Windows 10 Macro to remove underscores, except in email address Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I would use autoformat to create hyperlinks, then do your search and replace, then restore the email displayed text based on the hyperlinks. The code would look like this...
Code:
Sub Macro1()
  Dim aHL As Hyperlink
  With Options
    .AutoFormatApplyHeadings = False
    .AutoFormatApplyLists = False
    .AutoFormatApplyBulletedLists = False
    .AutoFormatApplyOtherParas = False
    .AutoFormatReplaceQuotes = False
    .AutoFormatReplaceSymbols = False
    .AutoFormatReplaceOrdinals = False
    .AutoFormatReplaceFractions = False
    .AutoFormatReplacePlainTextEmphasis = False
    .AutoFormatReplaceHyperlinks = True
    .AutoFormatPreserveStyles = True
    .AutoFormatPlainTextWordMail = True
    .LabelSmartTags = False
  End With
  ActiveDocument.Range.AutoFormat

  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "_"
    .Replacement.Text = " "
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
  For Each aHL In ActiveDocument.Range.Hyperlinks
    If aHL.Type = msoHyperlinkRange Then
      aHL.TextToDisplay = Replace(aHL.Address, "mailto:", "")
    End If
  Next aHL
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 09-27-2018, 01:08 AM
macropod's Avatar
macropod macropod is offline Macro to remove underscores, except in email address Windows 7 64bit Macro to remove underscores, except in email address Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by TomDW View Post
The Word document is the result of merging fields, and apparently this doesn't automatically format the e-mail addresses...
It can (see the Mailmerge Tips and Tricks 'Sticky' thread - https://www.msofficeforums.com/mail-...ps-tricks.html), but that's another story. Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Hlnk As Hyperlink
With ActiveDocument
  With .Range
    'Activate email addresses as hyperinks
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Format = False
      .Wrap = wdFindStop
      .MatchWildcards = True
      .Text = "<[0-9A-?.\-_]{1,}\@[0-9A-?\-_]{1,}.[a-z]{2,3}>"
      .Replacement.Text = ""
      .Execute
    End With
    Do While .Find.Found
      i = i + 1
      .Hyperlinks.Add Anchor:=.Duplicate, Address:="mailto:" & .Text, TextToDisplay:=.Text
      .Start = .Hyperlinks(1).Range.End
      .Find.Execute
    Loop
    'Replace underscores with spaces
    With .Find
      .Text = "_"
      .Replacement.Text = " "
      .Wrap = wdFindContinue
      .MatchWildcards = False
      .Execute Replace:=wdReplaceAll
    End With
  End With
  'Repair email address underscores
  For Each Hlnk In .Hyperlinks
    With Hlnk
      If InStr(.Address, "mailto:") > 0 Then .TextToDisplay = Split(.Address, "mailto:")(1)
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 09-27-2018, 01:15 AM
TomDW TomDW is offline Macro to remove underscores, except in email address Windows 10 Macro to remove underscores, except in email address Office 2016
Novice
Macro to remove underscores, except in email address
 
Join Date: Sep 2018
Posts: 3
TomDW is on a distinguished road
Default

Hi everyone, thanks for the replies.
Gmayor's code seems to do the trick...
If I'm having problems with this solution later on, I can always try the other suggestions.
Thanks!
Reply With Quote
Reply

Tags
macro replace email



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hide Email Address of Previous Email when Replying or Forwarding bondingfortoday Outlook 0 03-05-2016 04:29 PM
Add link to email address that hides the actual address or makes it inaccessible to online bots richiebabes Word 1 09-03-2014 03:22 PM
Macro to send from non-default email address k.n. Mail Merge 5 12-03-2013 03:22 AM
Mail Merge Many URLs in one email by common email address instantaphex Mail Merge 3 04-29-2013 05:46 PM
Remove a Group Email address from Outlook meppwc Outlook 0 10-17-2012 07:09 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:26 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft