Search For String And Print

  • Re: Search For String And Print


    Thanks again for all the help.


    One last question, if there are multiple word documents you need to print off, what would the best way for the user to select them and print the W5WW and W6WW without having to open each document and run the macro.

  • Re: Search For String And Print


    You could use the builtin FileOpen dialog. This will open all selected files and the macro could then process each one printing those that have the text.


    Or you could use DIR() and a loop to process all the files in a folder.


    Either way you will need to open the document in order to search and print it.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    I found the open dialog you posted on another thread but how would I select multiple files? This seems to only open 1 file at a time.


    Code
    Sub OpenDialog()
        Dim dial As Dialog
    
    
        Set dial = Dialogs(wdDialogFileOpen)
        dial.Name = "*.*"
        dial.Show
    End Sub
  • Re: Search For String And Print


    Alright, that code worked nice. It allows you to select multiple word docs and have them open.


    I am having trouble being able to the run the macro from earlier today. Once I have the files opened, how do I go about running the macro? I currenlty have a little userform setup but I want that macro to run after all documents are opened up.


    Suggestions? If you need to me upload something to view, let me know.


    Here's my code as of now. Its almost like I need a loop or something because it opens (x) amount of docs but only prints 1 of those (x) amount.


  • Re: Search For String And Print


    Not tested.


    You need to loop though all the open documents and process them. You probably need to exclude the document that contains the code.


    I have put the find part of the code in to a function.
    [vba]Sub Main()


    Dim docTemp As Document


    ' open all documents selected by user
    CommandBars.FindControl(ID:=23).Execute

    If Application.Documents.Count > 1 Then
    For Each docTemp In Application.Documents
    If docTemp.FullName <> ThisDocument.FullName Then

    strPage = MyFind(docTemp, "W5WW", "1101...5")
    If strPage <> "" Then
    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

    strPage = MyFind(docTemp, "W6WW", "1101...5")
    If strPage <> "" Then
    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

    docTemp.Close False
    End If
    Next
    End If


    End Sub
    Function MyFind(MyDoc As Document, FirstItem As String, EndItem As String) As String


    Dim strPage As String

    With MyDoc.Select
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = FirstItem
    .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 = EndItem
    .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
    End With

    If strPage <> "" Then
    ' Remove Trailing Comma
    strPage = Left(strPage, Len(strPage) - 1)
    End If

    MyFind = strPage

    End Function[/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    Any ideas on a compile error on the MyDoc.Select portion? I've searched awhile and can't seem to see what I'm missing.

  • Re: Search For String And Print


    Problem fixed. I took out the MyDoc.select and it worked.


    Hopefully this is the last we speak to each other! HA!


    Again, thanks for the help.

  • Re: Search For String And Print


    One way would be store temporarily set the default folder.
    [vba] Dim strOrigPath As String
    strOrigPath = Options.DefaultFilePath(Path:=wdDocumentsPath)
    Options.DefaultFilePath(Path:=wdDocumentsPath) = "C:\temp\"
    CommandBars.FindControl(ID:=23).Execute
    Options.DefaultFilePath(Path:=wdDocumentsPath) = strOrigPath
    [/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    I know this is probably getting out of my knowledge here but is there any way to execute a macro outside of word? I mean its easy enough to open word, and click on the macro and it runs itself. I'm sure people where I work will bitch cause they have to open word each time to execute.


    A buddy mine said vbscript, which I am not familiar with. Any other easy ways?

  • Re: Search For String And Print


    But you need to open Word anyway in order to print the documents.


    Possible to convert code to VB script but not sure what the saving would be.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    I agree on the opening of word anyways. They'll look at it as give us an icon/exe to click on which will open word for us automaticly and process each file and close word.


    Well I think I'll leave it the way it is now and they can convert to vbscript if they want.

  • Re: Search For String And Print


    Solution found. I just threw together a quick batch file which opens word, then each user clicks on on the marco on their toolbar (once its installed on each machine) and it runs the macro and tada, all selected documents printed off.


    1.) How would I installed this marco on other machines?


    2.) Is there a command to close word automaticly after the macro is finished?

  • Re: Search For String And Print


    Use the Open event of the document with the macro in.


    [vba]Private Sub Document_Open()


    ' replace with with a call to the macro
    MsgBox "MyMacro"

    End Sub[/vba]


    The you just need to give out the file and batch file. In fact just opening the doc file will start the process.


    [vba]Application.Quit[/vba] should shut down word.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Search For String And Print


    Slight problem. Take a look at the code. If a user selects only 1 file, the only thing it does it opens the document up but skips the printing of the W5WW and W6WW. Is there something I have missed?


Participate now!

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