PDA

View Full Version : Solved: how to print a page based on the text string found on the page?



bantumi12
11-11-2008, 08:21 AM
I want to search for a text string in a MS word document and print only the page which has the matching text. Is there any way to do it?

Demosthine
11-11-2008, 04:02 PM
Good Afternoon.

You'll want to use the Selection.Find Method. After completing the Find.Execute, make sure you use Selection.Extend to select the found text. Then you can use ThisDocument.PrintOut with Range set to wdPrintCurrentPage.


Selection.Find.ClearFormatting
With Selection.Find
.Text = "Tasha"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.Extend

ThisDocument.PrintOut Range:=wdPrintCurrentPage


Good luck.
Scott

fumei
11-12-2008, 01:46 PM
Selection.Find does select the .Found text. There is no need to use .Extend. Extend is only used with OTHER actions on the Selection. The method Find does this automatically.

This is one of the few cases where, in fact, Selection is better. But only for the reason that you wish to print the current page. Current page is defined as the page the Selection is on.

It could be done with a Range object, but it would take a little more coding. With Selection:

Sub yadda()
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
Do While .Execute(FindText:="yadda", _
Forward:=True) = True
Application.PrintOut _
Range:=wdPrintCurrentPage
Loop
End With
End With
End Sub


Note this prints every page that has "yadda" on it.

You can expand the process. Say you have three different words, and you want to print all the pages that have those words.

NOTE!!!!!!!! The following does NOT take into account pages that have multiple instances. In other words, if a given page has Word_A and Word_B, it would get printed twice. This could be fixed...it only a question of working out the logic. the coding is trivial.

Sub yadda()
dim myWords()
Dim j As Long
myWords = Array("Word_A", "Word_B" _
"Word_C", "Word_D")

For j = 0 To Ubound(myWords())
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
Do While .Execute(FindText:=myWords(j), _
Forward:=True) = True
Application.PrintOut _
Range:=wdPrintCurrentPage
Loop
End With
End With
Next j
End Sub

bantumi12
11-18-2008, 05:07 AM
wow... that was awesome guys.....tanks a lot...!

I have one more problem....

is there any way to findout in which page (i.e. the page number) the text is found.
For Example a stirng "Test" is availabel in only in page number 5, then is it possible to programmatically findout the text is found in page number 5?

regards

fumei
11-18-2008, 12:22 PM
Yes.

Using Selection:
Selection.Information(wdActiveEndAdjustedPageNumber)

bantumi12
11-18-2008, 09:40 PM
Thanks a lot fumei.....!

Let us say based on some algorithm I decide to print the pages in the following order 3,2,5,1,4. How can I fire a print for these pages in one go..?

fumei
11-19-2008, 12:56 PM
This is an entirely different question. Your post is regarding printing pages that contain a specific text string.

This question is regarding printing pages in a given order.

So. Do you want to print by a searched string...or by a given order?

Or both...somehow?

If your question is, in fact, different (how do I print pages in a given order), then please post a separate thread.

The answer to the question though is, yes...you can. Although - if and when you start a separate thread - it depends on exactly what you wish to do. So please give details. Is the order going to be hard coded?

Instruction: Print pages 3,2,5,1,4

Or will the order be derived from some other logic?

IF Page_1 has "yadda" THEN
IF Page_3 has "blah" THEN
Print pages 3,1
ELSE ' Page_1 has "yadda", Page_3 does NOT have "blah"
Print pages 1,3
END IF
END IF

...or whatever.

You can print however you want to print, basically.
Dim myPages()
Dim j As Long

myPages = Array(3, 2, 5, 1, 4)

For j = 0 To UBound(myPages())
Application.PrintOut FileName:="", _
Range:=wdPrintRangeOfPages, _
Item:=wdPrintDocumentContent, _
Copies:=1, Pages:=myPages(j)
Next j

would print pages in the order 3, 2, 5, 1, 4.

bantumi12
11-19-2008, 11:50 PM
Thanks a lot fumei...

Actually my problem includes searching a list of strings in a word document, sort the pages based on the order of strings and print them. i do agree that i did not phrase the problem properly. anyway thatks for the solution. with this input i can go ahead and code it.

Thanks a lot.. ;-)