Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-24-2019, 07:38 AM
PWH68 PWH68 is offline Extracting Specific Text Strings Windows 10 Extracting Specific Text Strings Office 2016
Novice
Extracting Specific Text Strings
 
Join Date: Sep 2019
Posts: 4
PWH68 is on a distinguished road
Default 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.


Reply With Quote
  #2  
Old 09-24-2019, 08:50 PM
gmayor's Avatar
gmayor gmayor is offline Extracting Specific Text Strings Windows 10 Extracting Specific Text Strings Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 09-26-2019, 03:30 AM
PWH68 PWH68 is offline Extracting Specific Text Strings Windows 10 Extracting Specific Text Strings Office 2016
Novice
Extracting Specific Text Strings
 
Join Date: Sep 2019
Posts: 4
PWH68 is on a distinguished road
Default

Thank you very much Graham
Reply With Quote
  #4  
Old 09-26-2019, 07:09 AM
gmaxey gmaxey is offline Extracting Specific Text Strings Windows 10 Extracting Specific Text Strings Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 10-01-2019, 01:28 AM
PWH68 PWH68 is offline Extracting Specific Text Strings Windows 10 Extracting Specific Text Strings Office 2016
Novice
Extracting Specific Text Strings
 
Join Date: Sep 2019
Posts: 4
PWH68 is on a distinguished road
Default Suggestions

Quote:
Originally Posted by gmaxey View Post
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.




So what would you suggest as an improvement please?
Reply With Quote
  #6  
Old 10-02-2019, 12:45 AM
Guessed's Avatar
Guessed Guessed is offline Extracting Specific Text Strings Windows 10 Extracting Specific Text Strings Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 10-08-2019, 02:07 AM
PWH68 PWH68 is offline Extracting Specific Text Strings Windows 10 Extracting Specific Text Strings Office 2016
Novice
Extracting Specific Text Strings
 
Join Date: Sep 2019
Posts: 4
PWH68 is on a distinguished road
Default Thanks

Quote:
Originally Posted by Guessed View Post
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



Thank you
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Specific Text Strings 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
Extracting Specific Text Strings Need help extracting specific text from one doument to another with macro/VBA. zsmithku Word 1 04-15-2011 03:46 PM
Extracting Specific Text Strings update style of all strings available between two specific strings vikrantkale Word 1 03-28-2011 06:13 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft