Consulting

Results 1 to 9 of 9

Thread: help with VBA code word after merge

  1. #1

    help with VBA code word after merge

    Hello,

    Can someone help me please concerning the following:

    I need a vba code that does the following, after merge I want to print the selected records to my chosen printer and then go to the next record and print it again.

    I have already this in my vba code
    Attached Files Attached Files

  2. #2
    It is not at all clear what you are trying to do. Why not simply merge to the printer the records that you actually want printed?
    Or you could use  https://www.gmayor.com/MergeAndSplit.htm to split the merge (selected records) to separate documents and use a macro to print those records.
    The following macro when used with the above will do that.

    Sub PrintDoc(oDoc As Document)Dim sPrinter As String
        With Dialogs(wdDialogFilePrintSetup)
            sPrinter = .Printer
            .Printer = "PRE-228202"
            .DoNotSetAsSysDefault = True
            .Execute
            oDoc.PrintOut
            .Printer = sPrinter
            .Execute
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    
    'Or even simply
    
    
    Sub PrintDoc(oDoc As Document)
        oDoc.PrintOut
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3

    help-with-VBA-code-word-after-merge

    Hello
    first of all thank you for the reply.
    the page (merge) in word has 4 same pages and my printer puts a staple after the 4 pages. (What I want ). After the merge Word sees it as one document and donĒt staple anymore . I tried several things and than I noticed that the way (print one record one by one) so this way it is working for me and I donít have to save this merge. Hope you understand my explanation
    thank you

  4. #4
    And I wanted to keep it simpele ��

  5. #5
    OK, I see the problem. What you could do is merge to a new document and then print in batches of four pages e.g.

    Sub SplitMergeLetterToPrinter()
    'Graham Mayor - https://www.gmayor.com - Last updated - 21 Jan 2021 
    Dim lngCounter As Long
    Dim sPrinter As String
    
    Const lngStep As Long = 3
    
        With Dialogs(wdDialogFilePrintSetup)
            sPrinter = .Printer
            .Printer = "PRE-228202"
            .DoNotSetAsSysDefault = True
            .Execute
        End With
    
         For lngCounter = 1 To ActiveDocument.Sections.Count Step lngStep
            ActiveDocument.PrintOut Background:=False, Range:=wdPrintFromTo, _
                                    from:="s" & Format(lngCounter), To:="s" & Format(lngCounter + lngStep)
            lngCounter = lngCounter + 1
        Next lngCounter
    
        With Dialogs(wdDialogFilePrintSetup)
            .Printer = sPrinter
            .Execute
        End With
    
    lbl_Exit:
        Exit Sub
    End Sub
    Or if you want to print each page as a separate print job, remove the step e.g.
    Sub SplitMergeLetterToPrinter()
    'Graham Mayor - https://www.gmayor.com - Last updated - 21 Jan 2021
    Dim lngCounter As Long
    Dim sPrinter As String
    
        With Dialogs(wdDialogFilePrintSetup)
            sPrinter = .Printer
            .Printer = "PRE-228202"
            .DoNotSetAsSysDefault = True
            .Execute
        End With
    
        For lngCounter = 1 To ActiveDocument.Sections.Count
            ActiveDocument.PrintOut Background:=False, Range:=wdPrintFromTo, _
                                    from:="s" & Format(lngCounter), To:="s" & Format(lngCounter)
            lngCounter = lngCounter + 1
        Next lngCounter
    
        With Dialogs(wdDialogFilePrintSetup)
            .Printer = sPrinter
            .Execute
        End With
    
    lbl_Exit:
        Exit Sub
    End Sub
    If you want simple the method I suggested previously will print each document as a separate print job.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    Thank you gmayor

    i am going to try this tomorrow and let you know of it works ��

  7. #7
    Goodmorning,

    maybe a stupid question but where put I the macro, in the page (template) or always in the page after the merge?
    The data always changes every day so I have to print every day the merge.
    I tried to put it in the template and I receive also an error in green.

    thank you for the help!

    Sub SplitMergeLetterToPrinter()
    'Graham Mayor - https://www.gmayor.com - Last updated - 21 Jan 2021
    Dim lngCounter As Long
    Dim sPrinter As String


    Const lngStep As Long = 3


    With Dialogs(wdDialogFilePrintSetup)
    sPrinter = .Printer
    .Printer = "PRE-228202"
    .DoNotSetAsSysDefault = True
    .Execute
    End With


    For lngCounter = 1 To ActiveDocument.Sections.Count Step lngStep
    ActiveDocument.PrintOut Background:=False, Range:=wdPrintFromTo, _
    from:="s" & Format(lngCounter), To:="s" & Format(lngCounter + lngStep)
    lngCounter = lngCounter + 1
    Next lngCounter


    With Dialogs(wdDialogFilePrintSetup)
    .Printer = sPrinter
    .Execute
    End With


    lbl_Exit:
    Exit Sub
    End Sub

  8. #8
    Hello,

    problem with execute is solved but now I have another issue

    When the printing job is done, there is one page that comes out seperately and that is the 3th page of the last record. Can someone help me with that please?

    thank you

  9. #9
    gmayor: can you help me with the above problem please?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •