VBA - Save as PDF using ListBox

Lee Rabbit

New Member
Joined
Apr 30, 2020
Messages
43
Office Version
  1. 2010
Platform
  1. Windows
Hi, I have run into a bit of difficulty with a macro I am running that creates a print preview of selected worksheets from a ListBox. Everything works fine and does exactly what I need with regards to printing the selected worksheets.

However, I am also looking to save the selected sheets as one PDF into "C:\PDF\EMAIL" with values from 3 cells with underscore EG: A1 & "_"

This is the code up to the PrintPreview. Is there anyway of expanding on this to create the PDF file?

VBA Code:
Sub print_sh()

    Dim i As Long, c As Long
    Dim SheetArray() As String
    
    With ActiveSheet.ListBoxSh
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve SheetArray(c)
                SheetArray(c) = .List(i)
                c = c + 1
                
            
            End If
            
        
        Next i
        
        
    
    End With
    Sheets(SheetArray()).PrintPreview
        
        
End Sub


To anyone who can help, a big thanks in advance.
Lee
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
sDir = "C:\myFolder\"
sFile = sDir & "Output.pdf"
Sheets.Select 'select all sheets
Sheets(1).Activate

ChDir sDir
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= sFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= False
 
Upvote 0
sDir = "C:\myFolder\"
sFile = sDir & "Output.pdf"
Sheets.Select 'select all sheets
Sheets(1).Activate

ChDir sDir
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= sFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= False

Hi, thanks for the reply. However, this process will select all sheets in the workbook and create a PDF based on that.

I am trying to select the values based on the SheetArray to combine into a single PDF.

My ListBox is populated with the active sheets in my workbook. So, if I want to create a print file of maybe 5 sheets out of the 40 listed, I just select the items I need from the ListBox, hit the form button and it will create a print preview.

My problem is that I would like the same form button to save the current selected sheets as a PDF in my chosen directory by using cell values.

VBA Code:
Filename:="C:\PDF\" & Range("N2").Text & "_" & Range("P2").Text & "_" & Range("Q2").Text & ".pdf", _

Cheers,
Lee
 
Upvote 0
First select all the sheets specified in SheetArray, and then export them to PDF using the ExportAsFixedFormat method of the ActiveSheet object...

VBA Code:
    'set the save folder (change accordingly)
    Dim saveFolder As String
    saveFolder = "C:\Users\Domenic\Desktop\"
   
    'set the saveas filename (change accordingly)
    Dim saveFilename As String
    saveFilename = Range("a1").Value & "_" & Range("b1").Value & "_" & Range("c1").Value & ".pdf"
   
    'select the specified sheets
    Sheets(SheetArray()).Select
   
    'export the selected sheets to PDF
    ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=saveFolder & saveFilename
       
    'de-select the selected sheets
    Sheets(SheetArray(0)).Select

Note that if a file with the same name already exists in the folder, it will be overwritten.

Hope this helps!
 
Upvote 0
First select all the sheets specified in SheetArray, and then export them to PDF using the ExportAsFixedFormat method of the ActiveSheet object...

VBA Code:
    'set the save folder (change accordingly)
    Dim saveFolder As String
    saveFolder = "C:\Users\Domenic\Desktop\"
  
    'set the saveas filename (change accordingly)
    Dim saveFilename As String
    saveFilename = Range("a1").Value & "_" & Range("b1").Value & "_" & Range("c1").Value & ".pdf"
  
    'select the specified sheets
    Sheets(SheetArray()).Select
  
    'export the selected sheets to PDF
    ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=saveFolder & saveFilename
      
    'de-select the selected sheets
    Sheets(SheetArray(0)).Select

Note that if a file with the same name already exists in the folder, it will be overwritten.

Hope this helps!

Domenic, you are a legend sir!!! Works a treat.

3 days I have been trying to get this to work and now I look at your workaround, I see where I was going wrong. Simple mistake my end. DOH!!!
 
Upvote 0

Forum statistics

Threads
1,215,437
Messages
6,124,871
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top