+ Reply to Thread
Results 1 to 8 of 8

Extract sentences containing a keyword and output results to Excel

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-06-2009
    Location
    London, England
    MS-Off Ver
    Microsoft 365, Excel, Version 2409
    Posts
    210

    Extract sentences containing a keyword and output results to Excel

    Hello.
    I found the attached code on Microsoft Community by Graham Mayor which searches a Word document (I'm using Office 365) for a keyword and outputs the sentence to Excel. It works really well. I was wondering if someone could extend the code so that it also outputs the line number and page number too - this would be really helpful

    Thank you

    
    Sub ExtractLine()
    Dim oPara As Paragraph
    Dim xlApp As Object
    Dim xlBook As Object
    Dim NextRow As Long
    Dim oRng As Range
    Dim strWord As String
    Const strWorkbookname As String = "D:\My Documents\WorkbookName.xlsx"
        strWord = InputBox("Enter word to find")
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute("^l")
                oRng = vbCr
                oRng.Collapse wdCollapseEnd
            Loop
        End With
        On Error Resume Next
        Set xlApp = GetObject(, "Excel.Application")
        If Err Then
            Set xlApp = CreateObject("Excel.Application")
        End If
        On Error GoTo 0
        Set xlBook = xlApp.Workbooks.Open(Filename:=strWorkbookname)
        xlApp.Visible = True
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(strWord)
                NextRow = xlBook.Sheets(1).Range("A" & xlBook.Sheets(1).Rows.Count).End(-4162).Row + 1
                oRng.Start = oRng.Paragraphs(1).Range.Start
                oRng.End = oRng.Paragraphs(1).Range.End - 1
                xlBook.Sheets(1).Range("A" & NextRow) = oRng.Text
                oRng.Collapse wdCollapseEnd
            Loop
        End With
    End Sub

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,830

    Re: Extract sentences containing a keyword and output results to Excel

    After:
                xlBook.Sheets(1).Range("A" & NextRow) = oRng.Text
    Insert:
                xlBook.Sheets(1).Range("B" & NextRow) = oRng.Information(wdActiveEndPageNumber)
                xlBook.Sheets(1).Range("C" & NextRow) = oRng.Information(wdFirstCharacterLineNumber)
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Forum Contributor
    Join Date
    12-27-2021
    Location
    China
    MS-Off Ver
    Ms office 2019
    Posts
    276

    Re: Extract sentences containing a keyword and output results to Excel

    Hello, everyone. I accidentally browsed this webpage and found that this code is really practical.
    On this basis, I want to raise a demand. I wonder if anyone can answer it.
    According to the code of post # 1, you can only paste the code to the word document you want to find.
    Can you fix a blank word document to store the code, pop up the document selection box to select the file each time you execute the macro, and then other codes are the same?

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,830

    Re: Extract sentences containing a keyword and output results to Excel

    Quote Originally Posted by 302 View Post
    According to the code of post # 1, you can only paste the code to the word document you want to find.
    The code can be stored in any document or template. The only proviso when running the code is that the document you want to process must be the active one (i.e. on-screen when you run the code).

  5. #5
    Forum Contributor
    Join Date
    12-27-2021
    Location
    China
    MS-Off Ver
    Ms office 2019
    Posts
    276
    can I use the pop up window select file ?

  6. #6
    Forum Contributor
    Join Date
    07-06-2009
    Location
    London, England
    MS-Off Ver
    Microsoft 365, Excel, Version 2409
    Posts
    210

    Re: Extract sentences containing a keyword and output results to Excel

    Thank you - this works.
    I had worked out from internet research that wdActiveEndPageNumber is used to capture page numbers but wasn't sure how to implement. Great job.

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,830

    Re: Extract sentences containing a keyword and output results to Excel

    Not without rewriting parts of the code. For example, you might replace:
        strWord = InputBox("Enter word to find")
        Set oRng = ActiveDocument.Range
    with:
    Set oRng = Nothing
    With Application.Dialogs(wdDialogFileOpen)
      If .Show = -1 Then
        .AddToMru = False
        .ReadOnly = True
        .Visible = False
        .Update
        Set oRng = ActiveDocument.Range
      End If
    End With
    If oRng Is Nothing Then Exit Sub
    strWord = InputBox("Enter word to find")

  8. #8
    Forum Contributor
    Join Date
    12-27-2021
    Location
    China
    MS-Off Ver
    Ms office 2019
    Posts
    276

    Re: Extract sentences containing a keyword and output results to Excel

    wow, you are great , thanks a lot.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel. Extract word from sentences
    By erci74 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 10-22-2022, 05:01 PM
  2. Extract words (out of a list) from sentences
    By Orphblk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2020, 08:56 PM
  3. Replies: 0
    Last Post: 09-07-2015, 04:05 PM
  4. Excel VBA to scan a PDF for keyword and extract PDF Pages to single PDF File
    By Brawnystaff in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2014, 02:00 AM
  5. Find a requested word within sentences and output those sentences elsewhere
    By swfred2000 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-19-2014, 02:24 PM
  6. Keyword Searching in a Webpage and Tabulating the results in an Excel Sheet
    By veejar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-01-2012, 11:56 PM
  7. How can I extract sentences from this website
    By DenniGa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2009, 04:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1