Home > Enterprise >  Print multiple pages (loop in a worksheet) in one pdf
Print multiple pages (loop in a worksheet) in one pdf

Time:01-20

I have the following problem with a VBA-Script which I haven't written:

It's a loop where the loop fills the printarea of the ws with information (page 1 f.e) --> prints the page 1 --> fills the the printarea of the ws with the next information (page 2 f.e.) --> prints the page 2 and on and on and on. I do have the option to tell the loop from which page it should start and when to end:

  • InputBox("Auf welcher Seite soll der Ausdruck beginnen ?", MSGTitel) - On which page should the print start?
  • Ende = InputBox("Bis zu welcher Seite soll gedruckt werden ?", MSGTitel) - Until which page it should print?

** My goal is to have all the pages I want to be saved in one pdf - is that possible?**

Here is the code:

Private Sub CommandButton6_Click()

    Dim Start
    Dim Ende
    Dim X1 As Integer
    
    Dim ST1 As Integer
    Dim End1 As Integer
    
    X1 = 0
    
    Start = InputBox("Auf welcher Seite soll der Ausdruck beginnen ?", MSGTitel)
    
    If Start <> "" Then
        If IsNumeric(Start) = True Then
        
        Ende = InputBox("Bis zu welcher Seite soll gedruckt werden ?", MSGTitel)
        
            If Ende <> "" Then
                If IsNumeric(Ende) = True Then
                                    
                    If Start > Ende Then
                         ST1 = Ende
                         End1 = Start
                    Else
                         ST1 = Start
                        End1 = Ende
                    End If
                                    
                                    
                    Do Until ST1   X1 = End1   1
                        
                        Tabelle6.Cells(11, 2).value = (Val(ST1   X1) * 20) - 19
                        Label1.Caption = Val(ST1   X1)
                        Tabelle6.Cells(6, 31).value = Label1.Caption
                        
                        'Debug.Print Label1.Caption
                        Tabelle6.PrintOut
                            
                        X1 = X1   1

                    Loop
                        
                   
                End If
            End If
        End If
    End If
End Sub

Thanks for your help

Tried to change the print method or instead of printing to save it as a pdf but I struggle with the loop as all my pdfs only have one page in it

CodePudding user response:

It is not possible with PrintOut method, if your sheet Tabelle6 contains only one page. Your problem cannot be simply solved with VBA.

You can try other solution: Rewrite VBA-script so that your loop, instead of PrintOut method, copies contents of Tabelle6 on new sheet, one page after (below) previous page, and then prints the whole new sheet, after the loop is finished (you need to take care of page breaks on new sheet, so that print looks like what you expect)

  • Related