Need to find and move url text to another word document

Steven Reynolds
Steven Reynolds used Ask the Experts™
on
I need to find url text(with & without hyperlink) using multiple type of RegEx then if it's found import all url text to new word document from current active document. Is it possible in word VBA? Can anyone ​suggest code?
RegEx Types
Type 1: with https://
Type 2: without https://
Type 3: with www.
Type 4: without www.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

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

Regards

Author

Commented:
Hi Rgonzo1971
Thank you for your assist. Above code is working well but I need to retain paragraph style, font forecolor, & bold style as per original document. Is it possible?

Author

Commented:
I need to retain all styles like paragraph style, list style, etc., & font colors, & bold style as per original document. Is it possible?
Top Expert 2016

Commented:
Sorry can't help further
Top Expert 2016

Commented:
A Solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial