Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?

VBA Style & formatting

Posted on 2017-11-14
2
Low Priority
?
51 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 77

Expert Comment

by:GrahamSkan
ID: 42368335
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
ID: 42368622
The above code is not working it's only add a new document
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Join & Write a Comment

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

576 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