Impession PDF avec entêtes

Bonjour,

J'ai besoin de vos conseils.

Je fais un gros travail sur un fichier qui contient beaucoup de macro.

Mais je galère sur une partie qui se trouve sur la création d'un PDF. Le code fonctionne, mais je n'arrive pas à mettre mes entêtes à chaque page du PDF.

Je joins un fichier exemple qui a pour entête les lettres de A à Q.

J'aimerais retrouver ces entêtes à chaque page du PDF. Si cela est possible

Merci.

Sub ImpPdf()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim targetColumn As String
    targetColumn = "A"

    Dim lastRow As Long
    Dim i As Long
    lastRow = ws.Cells(ws.Rows.Count, targetColumn).End(xlUp).Row

    For i = lastRow To 1 Step -1
        If Not IsEmpty(ws.Cells(i, targetColumn)) Then
            Exit For
        End If
    Next i

    If i > 0 Then

        With ws.PageSetup
            .Orientation = xlLandscape ' Format paysage
            .Zoom = 52 ' Zoom à 52% pour ajuster à la page
            .LeftMargin = 0 ' Marges gauche à zéro
            .RightMargin = 0 ' Marges droite à zéro
        End With

        ' Exporter en PDF avec les paramètres de mise en page définis
        ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Liste des matériels.pdf", Quality:=xlQualityStandard, _
                               IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True, _
                               From:=1, To:=i

        ws.PageSetup.CenterHeader = "" ' Réinitialiser l'en-tête
    Else
        MsgBox "La colonne ne contient aucune valeur.", vbExclamation, "Avertissement"
    End If
End Sub

Sub MsgPdf()
    Dim response As VbMsgBoxResult
    response = MsgBox("Voulez-vous créer un PDF ?", vbYesNo + vbQuestion, "Création de PDF")
    If response = vbYes Then
        ImpPdf
    End If
End Sub
9test-pdf.xlsm (24.71 Ko)

Bonjour Tespark

Sujet intéressant, identifier l'entete faire une macro qui coupe en paquets de lignes dans excel ou qui crée des tableaux dans Word avec un saut de page?

Sinon avec ws.PageSetup

tester

.PrintHeadings = True

.PrintTitleRows = "$1:$1"

https://support.microsoft.com/en-us/office/print-headings-or-titles-on-every-page-96719bd4-b93e-4237...

Il est dit : Under Print Titles, click in Rows to repeat at top

Bonjour,

Merci pour cette approche.

Sur le fichier test j'ai fait la manip et je sors ce code qui fonctionne très bien.

Sub ImpPDF()
     On Error GoTo ErrorHandler

    Dim ws As Worksheet
    Set ws = Feuil1

    Dim lastRow As Long
    Dim lastColumn As Long
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    lastColumn = ws.Cells(4, ws.Columns.Count).End(xlToLeft).Column

    Dim rng As Range
    Set rng = ws.Range("A4", ws.Cells(lastRow, lastColumn))

    With Feuil1.PageSetup
        .PrintTitleRows = "$4:$4"
        .PrintTitleColumns = ""
         .LeftMargin = Application.InchesToPoints(0)
        .RightMargin = Application.InchesToPoints(0)
        .TopMargin = Application.InchesToPoints(0.748031496062992)
        .BottomMargin = Application.InchesToPoints(0.748031496062992)
        .HeaderMargin = Application.InchesToPoints(0.31496062992126)
        .FooterMargin = Application.InchesToPoints(0.31496062992126)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlLandscape
        .PaperSize = xlPaperA4
        .Order = xlDownThenOver
        .Zoom = 52
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = True
    End With

    rng.Worksheet.PageSetup.PrintArea = rng.Address
    rng.Worksheet.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False, PrToFileName:=Environ("USERPROFILE") & "\Desktop\TestFichierPDF.pdf" ', PrToChangeFileName:=False

    Exit Sub

ErrorHandler:
    MsgBox "Une erreur s'est produite : " & Err.Description
End Sub

Le souci c'est sur mon fichier de travail la liste peut et sera surement à chaque fois avec un nombre de ligne indéterminé.

Ceci allant d'une centaines de lignes jusqu’à plusieurs milliers

Donc je ne peux pas utiliser cette mise en page de cette méthode, sauf erreur

Bonjour,

Je remonte ce sujet au cas ou quelqu'un aurait la solution.

Merci.

Rechercher des sujets similaires à "impession pdf entetes"