Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-06-2014, 09:12 AM
Hoxton118 Hoxton118 is offline VBA code to compile one document based on multiple search terms Windows 7 32bit VBA code to compile one document based on multiple search terms Office 2010 32bit
Novice
VBA code to compile one document based on multiple search terms
 
Join Date: Mar 2014
Posts: 21
Hoxton118 is on a distinguished road
Default VBA code to compile one document based on multiple search terms

I have the following code which allows me to select a search term and create a single new document which includes only those paragraphs containing the search term. I enter the search term in an input box.

Is there a way of getting the code to use a list of search terms from another file (whether Excel or Word) so that, rather than entering each search term individually, it will do multiple search terms at once - all results ending up in one document.



Another solution would be to enable a Boolean search in the input box, so that I could ask for multiple search terms there.

I would be very grateful for any help. Thanks.

Code:
Sub CreateSummary()
'
' CreateSummary Macro

Selection.Find.ClearFormatting
With Selection.Find
 .Text = InputBox("Type search term and press Enter.")
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.StartOf Unit:=wdParagraph
Selection.MoveEnd Unit:=wdParagraph
sBigString = sBigString + Selection.Text
Selection.MoveStart Unit:=wdParagraph

Loop
Documents.Add DocumentType:=wdNewBlankDocument
Selection.InsertAfter (sBigString)
End Sub
Reply With Quote
  #2  
Old 06-06-2014, 04:34 PM
macropod's Avatar
macropod macropod is offline VBA code to compile one document based on multiple search terms Windows 7 32bit VBA code to compile one document based on multiple search terms Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following. It assumes:
• you have a word list in another document, with each word/expression you want to find separated by paragraph breaks;
• the document to be processed in the active document; and
• a new document is to be created for the output.
Code:
Sub CreateSummary()
Application.ScreenUpdating = False
Dim FRDoc As Document, SrcDoc As Document, RsltDoc As Document, FRList, i As Long, Rng As Range
 'Load the strings from the reference doc into a text string to be used as an array.
Set SrcDoc = ActiveDocument
Set FRDoc = Documents.Open("Drive:\FilePath\ReferenceList.doc")
FRList = FRDoc.Range.FormattedText
FRDoc.Close False
Set FRDoc = Nothing
Set RsltDoc = Documents.Add
'Process each string from the reference doc
For i = 0 To UBound(Split(FRList, vbCr)) - 1
  With SrcDoc.Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .MatchWholeWord = True
      .MatchCase = True
      .Wrap = wdFindStop
      .Text = Split(FRList, vbCr)(i)
      .Execute
    End With
    Do While .Find.Found
      Set Rng = RsltDoc.Characters.Last
      Rng.Collapse wdCollapseStart
      Rng.FormattedText = .Duplicate.Paragraphs.First.Range.FormattedText
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
Application.ScreenUpdating = True
End Sub
Note: you will need to replace 'Drive:\FilePath\ReferenceList.doc' with the correct filepath & name for your reference document.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-09-2014, 02:40 AM
Hoxton118 Hoxton118 is offline VBA code to compile one document based on multiple search terms Windows 7 32bit VBA code to compile one document based on multiple search terms Office 2010 32bit
Novice
VBA code to compile one document based on multiple search terms
 
Join Date: Mar 2014
Posts: 21
Hoxton118 is on a distinguished road
Default

Many thanks Paul - that works a treat. Excellent.
Reply With Quote
  #4  
Old 04-04-2021, 04:56 AM
TullulahJoy TullulahJoy is offline VBA code to compile one document based on multiple search terms Windows 10 VBA code to compile one document based on multiple search terms Office 2019
Novice
 
Join Date: Apr 2021
Posts: 1
TullulahJoy is on a distinguished road
Exclamation Help - how to take this a step further....

Hi, I have a similar need which this macro almost works for (but not quite!)

1. Is there a way to retain the paragraph format & numbering when the search results are compiled in the new document? For context, the documents I am using this to search are parts of contracts, so maintaining the references for each paragraph is important. Currently when pasting the search results it currently resets the numbering to 1.1.1 etc. rather than maintain the source numbering.

2. How do I modify this to work even where a portion of the search document is a table? (structured as term in column one, definition in second column)

3. Is there a way to run the search using terms in an Excel document, and specifying a folder with several Word docs, and asking it to loop through the Word docs in that folder, performing the search? This would mean the results would need the title of the document to appear before the results from that document are compiled.


OR does all of this make it too distinct from the original ask, and should I post a new thread? Apologies, my first time here!
Reply With Quote
  #5  
Old 04-04-2021, 06:02 AM
macropod's Avatar
macropod macropod is offline VBA code to compile one document based on multiple search terms Windows 10 VBA code to compile one document based on multiple search terms Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by TullulahJoy View Post
OR does all of this make it too distinct from the original ask, and should I post a new thread? Apologies, my first time here!
Indeed it does - and you should.

With requirements as complex as yours, you'd do better to make a copy of the original document, then delete anything you don't want. The basic process would be:
1. Format all content as hidden text;
2. Find the desired content and unhide both that and the corresponding paragraph mark;
3. Convert all numbering to static numbering;
4. Find all hidden text and delete it; and
5. Optionally, delete all empty table rows.
There are plenty of threads here for each step. See, for example:
https://www.msofficeforums.com/125920-post3.html
https://www.msofficeforums.com/142904-post15.html
https://www.msofficeforums.com/34254-post4.html
https://www.msofficeforums.com/156087-post3.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code to compile one document based on multiple search terms Vba macro code for grouping the data based on 2 hours time dharani suresh Excel Programming 5 04-29-2014 03:25 AM
Search and replace/insert HTML code into Master File using tags dave8555 Excel 2 02-23-2014 03:51 PM
VBA code to compile one document based on multiple search terms Character based Search and Replace font size anacond11 Word 2 08-08-2013 08:10 AM
How do I search for terms in initial upper-case? bertietheblue Word 5 05-02-2012 05:24 AM
Modify vba code to print based on name in the InputBox OTPM Project 0 05-25-2011 02:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:32 PM.


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