Results 1 to 20 of 22

Thread: Find different expressions in same text (wildcard)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Apr 2020
    Posts
    12
    Location

    Exclamation Find different expressions in same text (wildcard)

    Hi, I would like to find expressions in a word document and extract them to a excel file.
    The text have some terms that can be useful to find the expressions between two parts.
    Like: 1. name Paul Sartre 2. id 20202-2 2.1 smoke ( x) yes ( ) no 4. ( x ) one day a week ( ) two days a week ( ) seven days a month.
    Thank you for answering this.

    I need to extract something like.

    find "name * 2. id" - result -> Paul Sartre
    find "2. id * 2.1" - result ->20202-2
    find "smoke * no 4." - result -> ( x) yes ( ) no
    find "4. ( * Thank you" - result -> ( x ) one day a week ( ) two days a week ( ) seven days a month.

    I find something useful, but I do not now how to put it in excel and how to add another expressions to search

    https://www.datanumen.com/blogs/extr...ument-another/

    Sub ExtractContentsBetweenTwoWords()
        Dim strFirstWord As String
        Dim strLastWord As String
        Dim objDoc As Document
        Dim objDocAdd As Document
        Dim objRange As Range
        ' Initialize and create a new blank document.
        Set objDoc = ActiveDocument
        Set objDocAdd = Documents.Add
        objDoc.Activate
        ' Enter the first and last words.
        strFirstWord = name
        strLastWord = 2. id
        ' Find and extract contents and insert them into the new document.
        With Selection
            .HomeKey Unit:=wdStory
            With Selection.Find
                .ClearFormatting
                .Text = strFirstWord & "*" & strLastWord
                .MatchWildcards = True
                .MatchWholeWord = True
                Do While .Execute
                    Selection.MoveStart Unit:=wdCharacter, Count:=Len(strFirstWord)
                    Selection.MoveEnd Unit:=wdCharacter, Count:=-Len(strLastWord)
                    objDocAdd.Range.InsertAfter Selection.Range & vbNewLine
                    Selection.Collapse wdCollapseEnd
                Loop
            End With
        End With
    End Sub
    Last edited by Aussiebear; 03-06-2025 at 03:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •