1

First off, I'm not very proficient at programming. I've got a few macros to help me with work, but I needed clear examples to make those.

I want to find any instances in a Word document that has multiple underscores and replace them with a set number of underscores. There could be anything from 30 to 5 underscores in a row, and I want to be able to find those and replace them with exactly 10 underscores. The underscores are most often in their own paragraph, but sometimes they're together with text.

I've had a look around on the site and online, but I can't see how to do it with an unknown number of underscores.

If someone could point me in the right direction, I'd be really grateful. Thanks.

braX
  • 11,506
  • 5
  • 20
  • 33
millman97
  • 125
  • 4
  • You don't actually need a macro, let alone RegEx, for this. All you need is a *wildcard* Find/Replace, Where Find = _{5,30} Replace = _________ – macropod Apr 05 '20 at 21:39
  • Can we close this question as offtopic like you have closed [mine](https://stackoverflow.com/questions/61636024/search-and-replace-with-wildcard-and-line-break)? –  May 10 '20 at 13:01

2 Answers2

1

All you need to do is to use wildcards!

This one line of code can do it:

ActiveDocument.Content.Find.Execute "_{5,30}", _
    MatchWildcards:=True, Replace:=wdReplaceAll, _
    ReplaceWith:="__________", Wrap:=wdFindContinue

Note:

The {5,30} part would leave underscores less than 5 or more than 30 untouched.

You can change it to {,30} to replace 30 underscores or less

Or {1,} to replace 1 or more underscores

Easy-to-follow article about wildcards can be found here.

Abdallah El-Yaddak
  • 440
  • 1
  • 9
  • 18
  • Ah, I didn't know about this. Thanks a lot! – millman97 Apr 05 '20 at 13:56
  • Does my response need any modification before you can endorse it as "Selected" for other people to learn from it? [Selecting an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Abdallah El-Yaddak Apr 05 '20 at 14:06
  • It appears from your comment that you are assuming that your answer will be selected. Isn't that a bit presumptuous? If your interest is that the OP will not select an answer unless prompted to do so I suggest you write something like the following, "After and period of time please consider selecting the answer that you find most helpful, assuming that at least one answer is helpful. There's no rush, however." Some may interpret your comment as pressuring the OP to select your answer. – Cary Swoveland Apr 05 '20 at 19:28
  • Thank you for the advice. I agree with you that the sentence you suggested is much better, but I want to mention that I already included a link that tells all about selecting an answer, which, when, and that it can be changed. Also, when I wrote it, there were only two answers, and the other answerer mentioned this answer as better. Also, that this comment (without the link) is copied from a 7.5k user [here](https://stackoverflow.com/questions/61004889/vba-syntax-for-selecting-range-objects/61005281?noredirect=1#comment107963977_61005491). I know that this last one is not an excuse though. – Abdallah El-Yaddak Apr 05 '20 at 19:59
0

you could

1) replace all "double" underscores to "single" ones till there lasts only "single" underscores

2) replace all "single" underscores to "_____"

With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting

    Do
        .Text = "__"
        .Replacement.Text = "_"
        .Wrap = wdFindContinue
        .Format = False
        .MatchWholeWord = False

        .Execute Replace:=wdReplaceAll
    Loop While .Found

    .Text = "_"
    .Replacement.Text = "_____"
    .Wrap = wdFindContinue
    .Format = False
    .MatchWholeWord = False

    .Execute Replace:=wdReplaceAll

End With

change Selection to your actual part of document you want to deal with (for instace ActiveDocument.Content for all document

HTH
  • 2,031
  • 1
  • 4
  • 10