Search For String And Print

  • Ok this may sound difficult and I've tried a few things but haven't been able to get it working.


    Anways, I want to be able to search for certain strings of text and print the corresponding pages related to them.


    Address - 3 Line and Address - 4 Line is the string I want to search for. I will always have that string within my word documents but the problem is they may fall at the beginning, middle or end of a document. Address - 3 Line and Address - 4 Line will always be grouped together. So when it find them, it prints just thos pages associated with each string. If need be, I can attached a dummyed up version of what I'm talking about.


    Any help would be greatly appreciated.

  • Re: Search For String And Print


    This should tell you the page the found text is on. Where selection is the searched for text.
    [vba]selection.Range.Information(wdActiveEndAdjustedPageNumber)[/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    That infomation is nice to know but I need to be able to print those pages out. Sometimes those records will be on pages 4-6 and other times they could be on 4-5 and so on.


    Any other suggestions?

  • Re: Search For String And Print


    So record a macro of you printing pages 4 and 5. Then use the information returned by my code example as a replacement for the specific pages numbers in your macro.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    I think my problem is I don't know how to or where to place your code to accomplish this task. See below for the current marco I have. This works for this specific document but as I mentioned, there will be a variety of "record lengths".


  • Re: Search For String And Print


    This will print the page that contains the search text.
    [vba]Sub Print_5and6_Records()
    '
    ' Print_5and6_Records Macro
    ' Macro recorded 9/29/2006 by

    Dim lngPage As Long
    '
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "W5WW"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    If Selection.Find.Found Then
    lngPage = Selection.Range.Information(wdActiveEndAdjustedPageNumber)

    Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:=CStr(lngPage), PageType:=wdPrintAllPages, _
    ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
    False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0

    End If


    End Sub[/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    Excellent job but I am missing one thing. That record continues to a second page. It printed out the first page fine but need the second page to print also. Marjority of the time it'll be on one page only but sometimes it will continue to a second page. How can I get that second page to print out?



    I did a search on "W5WW" and need a "W6WW" record also when the macro is run. Do I just repeat the code for the other search criteria?

  • Re: Search For String And Print


    Yes do another search and build a variable of the pages.


    [vba]Sub Print_5and6_Records()
    '
    ' Print_5and6_Records Macro
    ' Macro recorded 9/29/2006 by

    Dim strPage As String
    '
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "W5WW"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    If Selection.Find.Found Then
    strPage = CStr(Selection.Range.Information(wdActiveEndAdjustedPageNumber)) & ","
    End If

    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "W6WW"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    If Selection.Find.Found Then
    strPage = strPage & CStr(Selection.Range.Information(wdActiveEndAdjustedPageNumber)) & ","
    End If

    If strPage <> "" Then
    ' remove trailing comma
    strPage = Left(strPage, Len(strPage) - 1)

    Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:=strPage, PageType:=wdPrintAllPages, _
    ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
    False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0

    End If

    End Sub[/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    I appreciate the help! I am still not getting the second page that is corresponding to each search criteria. The current code is working fine, I am getting both search criterias just not the second page of each "record".

  • Re: Search For String And Print


    Testing using your example will only print 1 page as there is only 1 occurance of W5WW.


    I copied the data within the file and changed the second occurance to W6WW.


    The macro printed both pages on which the information was stored.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    Right, there will only be one instance of that per record but I need the remaining portion of that record to print also. Image there was other text on that second page. I deleted all text that was but I assume there is no way to print that second page along with the first page?

  • Re: Search For String And Print


    What about doing another search on lets say "1201...5". If the data get pushed out to a second page, could you doing another search on that and string the second search together with the first, thus printing pages 1 and 2? How can this be done, if it can even be done at all. I tested but only got the second search to print.

  • Re: Search For String And Print


    Ok here we go. Below is the code I currenlty have. How do I add 2 more search criterias for the W6WW record? The below code searches for W5WW and prints those 2 pages off, somewhat accomplishing the problem that still lies about getting that second page to print.


    Suggestions anyone?


  • Re: Search For String And Print


    If 1201...5 is the end of the record then yes.
    Also notice the chart to the page joining character, I have used a - instead of comma to specify a range of pages.
    [vba]Sub Print_5and6_Records()
    '
    ' Print_5and6_Records Macro
    ' Macro recorded 9/29/2006 by

    Dim strPage As String
    '
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "W5WW"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    If Selection.Find.Found Then
    strPage = CStr(Selection.Range.Information(wdActiveEndAdjustedPageNumber)) & "-"
    End If

    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "1201...5"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    If Selection.Find.Found Then
    strPage = strPage & CStr(Selection.Range.Information(wdActiveEndAdjustedPageNumber)) & "-"
    End If

    If strPage <> "" Then
    ' remove trailing comma
    strPage = Left(strPage, Len(strPage) - 1)
    MsgBox "Print Page: " & strPage

    Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:=strPage, PageType:=wdPrintAllPages, _
    ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
    False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0

    Else
    MsgBox "Nothing to print"
    End If

    End Sub[/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    Here's the one I just tested. It works fine but not sure if its the best way. How do I test for the error if "1201...5" doesn't exist? Meaning that the search for "W5WW" is only on one page? Does it just skip the "1201...5" search?


  • Re: Search For String And Print


    Yeah I took out the GoTo statements and add yours. I just thought of that problem just as you replied back.


    So what about the second search criteria? If it doesn't find it, does it go on to the other search criteria?


    I'll tell ya again, thanks for helping me out with getting this to work as far as we did. Just that last little bit a programming needed yet.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!