PDA

View Full Version : Save multiple ListBoxes to PDF



hokousai
05-21-2020, 03:56 PM
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

paulked
05-22-2020, 01:09 AM
This is the English language forum. Unless you want to ask again in English, try here http://www.vbaexpress.com/forum/forumdisplay.php?74-Non-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 :rofl:

hokousai
05-22-2020, 05:24 AM
Hello,
sorry i redo my request in english.
Thank you

hokousai
05-22-2020, 05:28 AM
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