-1

Here is the macro. It does the replacement as I require, but any bold or italic matches are replaced with plain text.

    Sub TEST_All_TEXT_CONVERT_COMMA_TO_HYPHEN_AT_PARAGRAPH_START()
    '
    ' TEST_All_TEXT_CONVERT_COMMA_TO_HYPHEN_AT_PARAGRAPH_START
    '
    '
Dim regExp As Object
Set regExp = CreateObject("vbscript.regexp")

With regExp
'Generic pattern
.Pattern = "(\r)([1-3 ]*[^ ]{1,15} )(\d+:\d+), (\d+\.)"
.Global = True
Selection.WholeStory
Selection = .Replace(Selection, "$1$2$3-$4")

End With
Selection.Collapse Direction:=wdCollapseEnd
'MsgBox "Done"
    End Sub
7
  • With some sample data it would be easier to help
    – Sam
    Commented Mar 4, 2019 at 16:07
  • RegEx is not useful when run on the content of a Word document when anything except the pure string is required in the result. You need to use Word's built-in Find/Replace, using "wildcards". There are lots of samples "out there". If you want help with a specific type of search you need to provide information about what you need to search, the required result, andsample data. Commented Mar 4, 2019 at 17:44
  • The VBA wildcard application in .Find/.Replace can be coded to support alternation, replacement or anything that you want. However, for us to help you, we need some clarity on what you are trying to achieve (what is the end effect) with some samples, rather than trying to fit your concept of the code to something else (X-Y problem).
    – AJD
    Commented Mar 4, 2019 at 19:11
  • Which part of the Question do you not understand? It does not require an example but I gave one. (deleted link now) How do I preserve bold, italic etc when using VB Regex in a Word macro
    – JPG
    Commented Mar 4, 2019 at 19:59
  • You should read Cindy's comment: RegEx is not useful when run on the content of a Word document when anything except the pure string is required in the result. Which part of that don't you understand?
    – macropod
    Commented Mar 4, 2019 at 21:37

1 Answer 1

1

Try this:

Sub TEST_All_TEXT_CONVERT_COMMA_TO_HYPHEN_AT_PARAGRAPH_START()
    Dim regExp As Object
    Set regExp = CreateObject("vbscript.regexp")

    With regExp
        'Generic pattern
        .Pattern = "(\r)([1-3 ]*[^ ]{1,15} )(\d+:\d+), (\d+\.)"
        .Global = True
        Selection.WholeStory
        'Selection = .Replace(Selection, "$1$2$3-$4")

        Dim Matches As Object
        Dim Match As Object

        Set Matches = .Execute(Selection)
        For Each Match In Matches
            Selection.Start = Match.FirstIndex
            Selection.End = Match.FirstIndex + Match.Length - 1
            Selection = .Replace(Selection, "$1$2$3-$4")
        Next Match
    End With
    Selection.Collapse Direction:=wdCollapseEnd
    'MsgBox "Done"
End Sub

I'm not perfectly sure it'll work, but I hope you get enough to solve your problem. Instead of treating all text in one go, it uses the Execute method to return a set of matches, which then are handled one by one.

2
  • Sorry it does not run because of the Next y and for some reason you have added an extra $4. But even with that fixed it did not replace the commas with hyphens, which it does in my posted example.
    – JPG
    Commented Mar 4, 2019 at 16:43
  • I've fixed the copy paste errors now. I will have a second look in a while.
    – Sam
    Commented Mar 4, 2019 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.