none
MAcro for Hypen & double hypen RRS feed

  • Question

  • I need a macro in a word file & search for single hypen & double hypen,

    if there are any single hypen in the documents there should be no space in a single hypen . If there is no space in the single space then it shoudl be ignored

    example :  Hel - lo  - should get converted to hel-lo

    If there are double hypen in the documents ,There should be one space before and after a double hypen.
    If there is any space between the hypen it should be deleted and if the space exist between the hypen as required then it should be ignored

    example :  pe--ople   - shoudl get converetd to po -- ople

        pe - - ople   - shoudl get converetd to po -- ople

    Kindly note that If there is any word having more than two hypen it shoudl be ignored, there shoudl not be any changes to it


    ALI

    Tuesday, December 18, 2012 6:17 AM

Answers

  • Try this macro:

    Sub ProcessHyphens()
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .MatchWildcards = True
            .Execute FindText:=" {1,}-", ReplaceWith:="-", Replace:=wdReplaceAll
            .Execute FindText:="- {1,}", ReplaceWith:="-", Replace:=wdReplaceAll
            .Execute FindText:="([! ])--", ReplaceWith:="\1 --", Replace:=wdReplaceAll
            .Execute FindText:="--([! ])", ReplaceWith:="-- \1", Replace:=wdReplaceAll
        End With
    End Sub


    Regards, Hans Vogelaar

    Wednesday, December 19, 2012 11:17 AM

All replies

  • It's not clear to me how to determine what a "word" is if it can contain spaces and hyphens...


    Regards, Hans Vogelaar

    Tuesday, December 18, 2012 4:38 PM
  • Sir,

    You have been great help to me.

    I am converting a pdf file to a huge word document. In the conversion some of the text are retain with extra spaces , I want a macro to help reduce the editing part of removing the unwanted space in the words associated with hypen.

    I hope I am able to clarify my point


    ALI

    Tuesday, December 18, 2012 6:15 PM
  • We, as humans, can easily decide what is a "word" in a string of characters containing spaces and hyphens. But it's much more difficult for a computer program. Take the following sentence:

    "The dog - a meat - eating animal - has been domesti - cated".

    How should a macro determine that "dog" and "a" do NOT belong together, but "meat" and "eating" do, while "animal" and "has" don't, and "domesti" and "cated" do? I have no idea how to do that.


    Regards, Hans Vogelaar

    Tuesday, December 18, 2012 8:21 PM
  • Sir, that was a good example . I will simply my requirement

    if there are any single hypen between the word in the documents there should be no space between the characters hypen . delete the space if any

    If there are double hypen between the characters in the documents ,There should be one space added before and after  the double hypen.

    if it is not possible ... Kindly reply with a no...

    Thanks a lot....


    ALI

    Wednesday, December 19, 2012 8:28 AM
  • Try:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Content.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchAllWordForms = False
      .MatchSoundsLike = False
      .MatchWildcards = True
      .Text = "[ ]{1,}-"
      .Replacement.Text = "-"
      .Execute Replace:=wdReplaceAll
      .Text = "-[ ]{1,}"
      .Replacement.Text = "-"
      .Execute Replace:=wdReplaceAll
      .Text = "([! ])(--)"
      .Replacement.Text = "\1 \2"
      .Execute Replace:=wdReplaceAll
      .Text = "(--)([! ])"
      .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
    End Sub

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, December 19, 2012 11:06 AM
  • Try this macro:

    Sub ProcessHyphens()
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .MatchWildcards = True
            .Execute FindText:=" {1,}-", ReplaceWith:="-", Replace:=wdReplaceAll
            .Execute FindText:="- {1,}", ReplaceWith:="-", Replace:=wdReplaceAll
            .Execute FindText:="([! ])--", ReplaceWith:="\1 --", Replace:=wdReplaceAll
            .Execute FindText:="--([! ])", ReplaceWith:="-- \1", Replace:=wdReplaceAll
        End With
    End Sub


    Regards, Hans Vogelaar

    Wednesday, December 19, 2012 11:17 AM