Consulting

Results 1 to 7 of 7

Thread: VBA to replace hyphens with en dashes

  1. #1
    VBAX Regular
    Joined
    Jan 2016
    Posts
    18
    Location

    VBA to replace hyphens with en dashes

    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 and dashes.


    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!

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. There is a setting (in 2016, maybe earlier) that does that while you type


    Capture.JPG




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


    Capture2.JPG


    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Jan 2016
    Posts
    18
    Location
    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.plusminus.docx

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Capture.JPG

    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



    Capture1.JPG


    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    Jan 2016
    Posts
    18
    Location
    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.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Jan 2016
    Posts
    18
    Location
    gmaxey,

    Very nice, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •