#1
|
|||
|
|||
Extracting Specific Text Strings
I want to write VBA to extract sentences or paragraphs that contain specific words into a new word document.
E.g. the phrase I am looking for is 'the supplier' and it could be at the start of a sentence or in the middle of a paragraph 1.1 The Supplier shall perform the Services sothat they meet or exceed the applicable Target Performance Levels at all times. 1.8 If the Supplier fails tomeasure or report on a Performance Indicator in accordance with therequirements placed upon it by Part B of this Schedule, the Supplier shall bedeemed to have failed to meet the Target Performance Level for the relevantPerformance Indicator in the relevant Measurement Period, unless the Authorityotherwise agrees in writing and, where the Performance Indicator has anapplicable Critical Failure Performance Threshold, the Supplier shall be deemedto have failed to meet that Critical Failure Performance Threshold. For the purposes of paragraphs 3 below, Service Points shall accrue to theSupplier in respect of that deemed Performance Failure and following therelevant Service Period at the highest available level associated with thatKPI. |
#2
|
||||
|
||||
The following will write sentences containing 'the supplier', regardless of case, to a new document.
Code:
Sub Macro1() 'Graham Mayor - https://www.gmayor.com - Last updated - 25 Sep 2019 Dim oSource As Document Dim oTarget As Document Dim oRng As Range, oTargetRng As Range Set oSource = ActiveDocument Set oTarget = Documents.Add Set oRng = oSource.Range With oRng.Find Do While .Execute(findText:="the supplier") oRng.Start = oRng.Sentences(1).Start oRng.End = oRng.Sentences(1).End Set oTargetRng = oTarget.Range oTargetRng.Collapse 0 oTargetRng.FormattedText = oRng.FormattedText oRng.Collapse 0 Loop End With Set oRng = Nothing Set oTargetRng = Nothing Set oSource = Nothing Set oTarget = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thank you very much Graham
|
#4
|
|||
|
|||
As Graham knows that can give patchy results. Consider:
Joe Smith and Dr. Robert Anderson are the suppliers for ... You will not get what you want. Check the results. |
#5
|
|||
|
|||
Suggestions
Quote:
So what would you suggest as an improvement please? |
#6
|
||||
|
||||
i would suggest you use the full paragraph instead of asking Word to determine what is a sentence. I haven't tested this but the following changes might do the job...
Replace: oRng.Start = oRng.Sentences(1).Start oRng.End = oRng.Sentences(1).End With: Set oRng = oRng.Paragraphs(1).Range
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
Thanks
Quote:
Thank you |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Renaming several strings of text by their pattern | noname91 | Word | 10 | 01-05-2017 05:46 PM |
Extracting lines containing specific word. | S.Allen | Word VBA | 1 | 03-27-2015 10:50 PM |
Extracting specific rows | sbdk82 | Excel | 4 | 09-07-2014 10:24 PM |
Need help extracting specific text from one doument to another with macro/VBA. | zsmithku | Word | 1 | 04-15-2011 03:46 PM |
update style of all strings available between two specific strings | vikrantkale | Word | 1 | 03-28-2011 06:13 PM |