VBA Style & formatting

I need to retain all styles like paragraph style, list styles, & font colors & bold formatting as per original document while search & move the regex-based text. Pls suggest code if is it possible.

Sub RegexSimple()
Dim regex As Object
Dim arrSentences()
Idx = -1
Set regex = CreateObject("VBScript.RegExp")

    With regex
        .Global = True
        .IgnoreCase = False
        .Pattern = "\b(?:https?:\/\/)?(?:www\.)?(?=\w+\.)[a-zA-Z0-9.\/]+\b"
    End With
    Set Mtchs = regex.Execute(ActiveDocument.Range.Text)
    For Each Mtch In Mtchs
        Idx = Idx + 1
        ReDim Preserve arrSentences(Idx)
        arrSentences(Idx) = Trim(Mtch)
    Next
    Documents.Add
    ActiveDocument.Range.Text = Join(arrSentences, vbCrLf)
Set regex = Nothing
End Sub

Open in new window

Steven ReynoldsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
Try using Word's Find object and Copy and Paste the found ranges.
 Note, I am not very fluent in Regex, so I can't be sure what your precise target is, but see how this goes.
Sub CopyFoundRanges()
    Dim docNew As Document
    Dim docA As Document
    Dim rng As Range
    Dim rng2 As Range
       
    Set docA = ActiveDocument
    Set docNew = Documents.Add
    Set rng = docA.Range
    With rng.Find
        .MatchWildcards = True
        .Text = "https:\/\/www\.[a-zA-Z0-9\-\.]{1,}>"
        Do While .Execute
            Set rng2 = rng.Duplicate
            rng2.Expand wdParagraph
            rng2.Copy
            docNew.Bookmarks("\EndOfDoc").Range.Paste
        Loop
    End With
End Sub

Open in new window

0
Steven ReynoldsAuthor Commented:
The above code is not working it's only add a new document
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.