Consulting

Results 1 to 4 of 4

Thread: Save multiple ListBoxes to PDF

  1. #1
    VBAX Regular
    Joined
    Mar 2020
    Location
    France
    Posts
    9
    Location

    Save multiple ListBoxes to PDF

    Bonjour,
    J'ai ce code qui m'a été donné par quelqu'un sur ce forum.
    Il vous permet d'enregistrer le contenu d'une ListBox.
    Seulement, il le fait pour un seul ListBox.
    Comme j'ai plusieurs ListBox, je ne trouve pas de moyen de modifier le code pour ajouter un code comme: ListBox1, ListBox2, etc ...
    J'aurais besoin de votre aide
    Merci d'avance
    Ps: je rejoint un fichier pour vous Pour mieux comprendre.
    Private Sub Enregistrer_1_seul_PDF_Click()    Dim i As Long
        Dim k As Long
        Dim varrSelected() As Variant
        Dim varrToSave  As Variant
        Dim shActiv     As Object
    
    
        k = -1
        Application.ScreenUpdating = False
        
        For i = 0 To ListBox2.ListCount - 1
            If ListBox2.Selected(i) Then
                k = k + 1
                ReDim Preserve varrSelected(0 To 1, 0 To k)
                varrToSave = varrToSave & "/" & ListBox2.List(i)
                varrSelected(0, k) = ListBox2.List(i)
                varrSelected(1, k) = ThisWorkbook.Sheets(varrSelected(0, k)).Visible
                ThisWorkbook.Sheets(varrSelected(0, k)).Visible = xlSheetVisible
            End If
        Next i
        If k > -1 Then
            Set shActiv = ActiveSheet
            varrToSave = Mid(varrToSave, 2)
            varrToSave = Split(varrToSave, "/")
            ThisWorkbook.Sheets(varrToSave).Select
            'Feuil2 is a CodeName of "Parametres" sheet
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="O:\" & Feuil2.Range("C2").Value & ".pdf"
            shActiv.Select
            For i = 0 To UBound(varrSelected, 2)
                ThisWorkbook.Sheets(varrSelected(0, i)).Visible = varrSelected(1, i)
            Next i
            
            MsgBox "Selected sheets were saved in a PDF file.", vbInformation
        End If
        
        Application.ScreenUpdating = True
    
    End Sub

    Attached Files Attached Files
    Last edited by hokousai; 05-21-2020 at 04:07 PM.

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    This is the English language forum. Unless you want to ask again in English, try here http://www.vbaexpress.com/forum/foru...n-English-Help

    ps My first language is Welsh, not English, but they don't have a Welsh forum... I doubt there is a call for one anyway
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Regular
    Joined
    Mar 2020
    Location
    France
    Posts
    9
    Location
    Hello,
    sorry i redo my request in english.
    Thank you

  4. #4
    VBAX Regular
    Joined
    Mar 2020
    Location
    France
    Posts
    9
    Location
    Hello,
    I have this code that was given to me by someone on this forum.
    It allows you to save the contents of a ListBox.
    Only it does that for a single ListBox.
    Since I have several ListBoxes, I can't find a way to modify the code to add a code like: ListBox1, ListBox2, etc. ...
    I would need your help
    Thank you in advance
    Ps: I am attaching a file for you to better understand.
    Private Sub Enregistrer_1_seul_PDF_Click()
        Dim i As Long
        Dim k As Long
        Dim varrSelected() As Variant
        Dim varrToSave  As Variant
        Dim shActiv     As Object
    
    
        k = -1
        Application.ScreenUpdating = False
        
        For i = 0 To ListBox2.ListCount - 1
            If ListBox2.Selected(i) Then
                k = k + 1
                ReDim Preserve varrSelected(0 To 1, 0 To k)
                varrToSave = varrToSave & "/" & ListBox2.List(i)
                varrSelected(0, k) = ListBox2.List(i)
                varrSelected(1, k) = ThisWorkbook.Sheets(varrSelected(0, k)).Visible
                ThisWorkbook.Sheets(varrSelected(0, k)).Visible = xlSheetVisible
            End If
        Next i
        If k > -1 Then
            Set shActiv = ActiveSheet
            varrToSave = Mid(varrToSave, 2)
            varrToSave = Split(varrToSave, "/")
            ThisWorkbook.Sheets(varrToSave).Select
            'Feuil2 is a CodeName of "Parametres" sheet
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="O:\" & Feuil2.Range("C2").Value & ".pdf"
            shActiv.Select
            For i = 0 To UBound(varrSelected, 2)
                ThisWorkbook.Sheets(varrSelected(0, i)).Visible = varrSelected(1, i)
            Next i
            
            MsgBox "Selected sheets were saved in a PDF file.", vbInformation
        End If
        
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

Posting Permissions

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