PDA

View Full Version : help with VBA code word after merge



Katy asking
01-20-2021, 09:25 AM
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

gmayor
01-20-2021, 09:42 PM
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

Katy asking
01-20-2021, 10:35 PM
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 :yes). 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 :bug:
thank you

Katy asking
01-20-2021, 10:37 PM
And I wanted to keep it simpele ��

gmayor
01-21-2021, 02:27 AM
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.

Katy asking
01-21-2021, 04:04 PM
Thank you gmayor

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

Katy asking
01-25-2021, 12:35 AM
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

Katy asking
01-29-2021, 05:19 AM
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

Katy asking
02-01-2021, 06:50 AM
gmayor: can you help me with the above problem please?