PDA

View Full Version : [SOLVED:] VBA to replace hyphens with en dashes



menteith
09-25-2016, 05:23 AM
Hi all,

I'd like to create a macro in Word (I use 2016) which replaces hyphenes with en-dashes ("a longer hyphen"). I think when I paste a hyphen in this forum I automatically becomes an dash. Wikipedia has a nice article on hyphens (https://en.wikipedia.org/wiki/Hyphen) and dashes (https://en.wikipedia.org/wiki/Dash).


Here's my code (recorded and edited my me):



Sub plusminus()
With Selection.Find
.Text = " - " ' it should be hyphen here but VBA editor and this forum changes it to a dash
.Replacement.Text = " – " ' en dash is here
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

When I try to copy and paste a hyphen to VBA editor I always get en dash. If I try to press a hyphen key on my keyboard I always get en dash. In consequence, the macro won't work. How do I replace hyphens en dashes using VBA?

Thanks!

Paul_Hossler
09-25-2016, 06:28 AM
1. There is a setting (in 2016, maybe earlier) that does that while you type


17162




2. On the Find/Replace dialog, there is a [Special] button that has that plus others. Click [More>>] to see it


17163


3. Recording a macro and using the [Special] button, you get a good starting point





Option Explicit
Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " - "
.Replacement.Text = " ^= "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub






4. You can also use




Replacement.Text = " " & Chr(150) & " "


for an en-dash, or 151 for an em-dash

menteith
09-25-2016, 06:56 AM
Many thanks! It seems that my character to be replaced is not a hyphen, that is, Chr(45) but something else. I ran you macro and apparently no modifications were made. Could you please take a look at my word document, please? A symbol I need to replace is in yellow on the first page.17164

Paul_Hossler
09-25-2016, 07:43 AM
17165

If you put the insertion point after the yellow character and hit Alt-x you can see the character's code in hex (2013). Alt-x to toggle back

2013 is the Unicode en-dash, which is slightly different from the hyphen (pink) hex 002D below



17166


If you want a longer dash, you can replace the en-dash (hex 2013) with an em-dash (hex 2014)


Option Explicit

Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " " & ChrW(&H2013) & " "
.Replacement.Text = " " & ChrW(&H2014) & " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

menteith
09-25-2016, 08:05 AM
Dear Paul_Hossler,

Many thanks. I would have never guessed it on my own. And the trick with alt-x is perfect! I really appreciate your answer.

gmaxey
09-25-2016, 05:35 PM
Paul/mentieth,

You can use the VBA Replace function to reduce that macro to one line of code:

Sub ScratchMacro()
ActiveDocument.Range.Text = Replace(ActiveDocument.Range.Text, ChrW(&H2013), ChrW(&H2014)")
End Sub

menteith
09-26-2016, 02:42 AM
gmaxey,

Very nice, thanks!