Go Premium for a chance to win a PS4. Enter to Win

x
?

VBA Style & formatting

Posted on 2017-11-14
2
Low Priority
?
38 Views
Last Modified: 2017-11-14
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

0
Comment
Question by:Steven Reynolds
2 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
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
 

Author Comment

by:Steven Reynolds
The above code is not working it's only add a new document
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Join & Write a Comment

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question