Solved

Word macro for printing only paragraphs with specified words

Posted on 2014-01-14
9
412 Views
Last Modified: 2014-01-26
I'm using Word in Office 2014 under Win 8.1 to do research involving very long documents (hundreds of pages) where I need to focus my searches on paragraphs that  contain at least one (of up to 20) words.

I need suggestions for macros to help me, as follows:

The Input:
(a) I would enter the (up to 20 words) into the macro.  Rather than being prompted, I'd enter them directly into the macro for each separate run of the macro.
(b) If it would be easier, I could insert into the macro code the total number of pages in the document to be researched for each run of the macro.

The words to researched must be case sensitive and both partial and whole in the same search -- not necessarily whole words (for example, I want to include suffices including, but not limited to, ...ing, ...ed, ...'s, etc.)

The Output:
(a)  The goal is produce a 2nd document for printing only the paragraphs where one or more of the target words occur.  Each of the target words in both docs should be highlighted to the then current highlight color (and left that way upon macro completion).

(b) It seems that the macro MUST (you decide) run two passes where the first pass highlights the (up to 20) target words and, for the second pass, prints each paragraph that contains at least one targeted word.

(c) Target paragraphs copied to the 2nd doc. should be inserted in their original sequence, without forced page breaks, but with a couple of blank lines in between (with all targeted words highlighted).  For each paragraph copied into the 2nd doc, I really need to know what page it came from in the original doc.  Perhaps the easiest way to capture this page info. might be to grab it from the location of the first word in each paragraph before the paragraph is copied to the 2nd doc.  That page number should be printed in the line before the paragraphs.

(d) Printing must not be automatic, but under my manual control, after the macro has completed and I am able to inspect the results.


Six years ago I could have done this fairly quickly in Word 2003 under XP, but I've been away too long, there have been multiple changes to MS Word and the OS since that time.

Appreciate the help and even partial code suggestions from one or more experts.

Thanks,

WS
0
Comment
Question by:WaterStreet
  • 5
  • 4
9 Comments
 
LVL 14

Expert Comment

by:DrTribos
ID: 39781416
If you reuse search words you could make use of a table in a control document to store them.  In-putting should not be an issue.  You might have many columns and select a column of words to search... this could be done using an input box.

I don't see a need to include the page range unless you specifically want to exclude some 'matches'.

Wild card search will help with the suffix, e.g. a search for <jump*> will also return jumping, jumps, jumped.

You will need to be clever with the wild card search expressions - there are some great introductions out there, see here.  here and here.

I am not sure what the fastest, most elegant way to execute a search - given that you are looking for any number of possible matches you need to use a greedy search (i.e. don't stop if you find something).

... and mid thought, I have to go.  Hope this gets you started.
0
 
LVL 14

Assisted Solution

by:DrTribos
DrTribos earned 500 total points
ID: 39781884
This should serve as a good starting point for the first pass...
Sub FindMyWords()
Dim findWord As Variant
Dim unitRng As Range
 
findWord = Array("word", "run", "jump", "swim")
ActiveDocument.Range.Select
MultipleReplace Selection.Range, findWord
End Sub
 
Sub MultipleReplace(rngArea As Range, findWord As Variant)
Dim i As Integer
    For i = LBound(findWord) To UBound(findWord)
        With Selection.Find
            .MatchWildcards = True
            .MatchCase = True
            .MatchWholeWord = True
            .Replacement.Text = ""
            .Replacement.Highlight = True
            .Execute FindText:="<" & findWord(i) & "[singed]{1,}>", replacewith:="", Replace:=wdReplaceAll
        End With
        
    Next i
End Sub

Open in new window


The approach is adapted from Graham Skans answer to one of my first EE questions... The wild card "<" & findWord(i) & "[psinged]{1,}>" is an array with with your word list ("findWord(i)") and the ("[singed]{1,}") takes care of the 'ing', 'ed', 's' - it was mildly tweaked for my test words.

There is a problem - it will find jumps, jumped, jumping but not jump... I'll look at that a bit more later.

[Edit] - might not be much point in tweaking until all the search words are known.
0
 
LVL 18

Author Comment

by:WaterStreet
ID: 39788811
Sorry.  I've been tied up on the phone with Microsoft for almost all of the last two days and five hand-offs (really) trying to solve an Access 2013 crash.  :-(

Thanks.  I'm reviewing your replies now.

WS
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39789761
Yikes - sounds fun.  The tricky part of what (I think) you want to do is the wildcard search.  In particular the article on the MVP website will reveal the trickery!
0
A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 18

Author Comment

by:WaterStreet
ID: 39795045
I could not get the macro to work at all for the following text of a word document when I used
findWord = Array("Illinois", "Pennsylvania", "Virginia", "New York", "Connecticut", "Ohio", "Massachusetts", "Northwest", "Michigan", "Minnesota", "Indiana", "Wisconsin", "Territory")

Shown below is a test you can use to verify the problem.  I got it from Wikipedia then I copied four times (with some modifications) as a test for the proposed macro.  Note that I changed the State of Michigan for this test by making it possessive with an apostrophe “s” in the 2nd and 4th paragraphs.

Please try my FindWord Array with the following that you can paste into a word document.

Note: My searches will always be case sensitive.


=========================================
The passage of the Northwest Ordinance in 1787. This ordinance accepted the abolishment of all claims to the land west of Pennsylvania and north of the Ohio River by the states of Pennsylvania, Virginia, New York, Connecticut, and Massachusetts, and the ordinance established Federal control over all of this land in the Northwest Territory—with the goal that several new states should be created there. In the course of time, this land was divided among Ohio, Michigan, Indiana, Illinois, Wisconsin, and part of Minnesota.

The passage of the Northwest Ordinance in 1787. This ordinance accepted the abolishment of all claims to the land west of Pennsylvania and north of the Ohio River by the states of Pennsylvania, Virginia, New York, Connecticut, and Massachusetts, and the ordinance established Federal control over all of this land in the Northwest Territory—with the goal that several new states should be created there. In the course of time, this land was divided among Ohio, Michigan’s, Indiana, Illinois, Wisconsin, and part of Minnesota.

The passage of the Northwest Ordinance in 1787. This ordinance accepted the abolishment of all claims to the land west of Pennsylvania and north of the Ohio River by the states of Pennsylvania, Virginia, New York, Connecticut, and Massachusetts, and the ordinance established Federal control over all of this land in the Northwest Territory—with the goal that several new states should be created there. In the course of time, this land was divided among Ohio, Michigan, Indiana, Illinois, Wisconsin, and part of Minnesota.

The passage of the Northwest Ordinance in 1787. This ordinance accepted the abolishment of all claims to the land west of Pennsylvania and north of the Ohio River by the states of Pennsylvania, Virginia, New York, Connecticut, and Massachusetts, and the ordinance established Federal control over all of this land in the Northwest Territory—with the goal that several new states should be created there. In the course of time, this land was divided among Ohio, Michigan’s, Indiana, Illinois, Wisconsin, and part of Minnesota.
==================================

Thanks in advance
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39795139
Change wildcards to false
Change .execute findtext:= findword (i)

Sorry for the brevity - using phone
0
 
LVL 18

Accepted Solution

by:
WaterStreet earned 0 total points
ID: 39795941
Your suggested change did not highlight.

However, I tinkered with some test macros that I recorded, in order to see what code they generated.

I finally decided to use Replace with the Replacement Text equal to the "Find What Text" argument (= ^&) that I remembered using in another macro long ago.

The MultipleReplace now works, as I modified it below (it still needs to be called by your unmodified FindMyWords sub):

Sub MultipleReplace(rngArea As Range, findWord As Variant)
Dim i As Integer
Dim SelFind As Variant

    For i = LBound(findWord) To UBound(findWord)
    SelFind = findWord(i)
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Highlight = True
    With Selection.Find
        .Text = SelFind
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
         Next i
End Sub

I'm thinking of awarding you the Assisted solution at full points (because, of the iteration technique that I learned for you, but award this posting as the Accepted Solution at zero points.  I will then close this question and ask the 2nd part in a new question.

What do you think?
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39796079
Thanks. That is fine. I didn't get a chance to test as I was using my phone.  Glad to have helped.
0
 
LVL 18

Author Closing Comment

by:WaterStreet
ID: 39809839
I awarding this Question as discussed above.


The remaining part is asked in a new thread at:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Q_28344319.html
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will show you how to use shortcut menus in the Access run-time environment.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now