VBA - Convert Excel Files to PDF

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Gurus,

Could you possibly help me build a codes for this one?

Basically, I have a folder with excel Files:

Folder Name: Data Sheets

The folder contains different sheets example:
1. Data 1.xls
2. Data 2.xlxs
3. Data 3.xls
4. Data 5.xlsx
..and so on

Is there any way, where I can convert each excel files into PDF? Also, can I have another code for merging the PDFs into one (consolidated)?

Any thoughts will be much appreciated. :)
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Your first request
- pdf given same file name as the original file
- problematic if folder contains .xls and .xlsx versions with same name
- amend both path strings (ending both with path separator "\")

VBA Code:
Sub CreatePdf()
    Dim Extn As String, wb As Workbook, wbNext As String, pdfName As String
    Const wbPath = "C:\TestArea\A1\"
    Const pdfPath = "C:\TestArea\A2\"
    Extn = "*.xls*"
   
    wbNext = Dir(wbPath & Extn)
    Do While wbNext <> ""
        Set wb = Workbooks.Open(wbPath & wbNext)
        DoEvents
        pdfName = Left(wbNext, InStrRev(wbNext, ".") - 1) & ".pdf"
        wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath & pdfName
        wb.Close SaveChanges:=False
        DoEvents
        wbNext = Dir
    Loop
End Sub

If you cannot get it working post your amended code in full
 
Upvote 0
Also, can I have another code for merging the PDFs into one (consolidated)?
How many workbooks are there ? How big are they ?
- one possibility is to loop through all sheets in all workbooks combining them into one big workbook and converting that to PDF
 
Upvote 0
How many workbooks are there ? How big are they ?
- one possibility is to loop through all sheets in all workbooks combining them into one big workbook and converting that to PDF

Thanks for the help. The initial code was helpful. :)

As for merging the PDFs, each file is around 300KB and varies from 25-50 files in a folder (one tab only). When merging the PDFs, it should be in order:

Example:
1. PDF
2. PDF
3. PDF
...and so on.

Thanks again!
 
Upvote 0
Amended procedure as requested

VBA Code:
Sub CreatePdf()
    Application.ScreenUpdating = False
    Dim Extn As String, wbNext As String, pdfName As String, wbTEMP As Workbook, wb As Workbook
    Const wbPath = "C:\TestArea\A1\"
    Const pdfPath = "C:\TestArea\A2\"
    Extn = "*.xls*"
    Set wbTEMP = Workbooks.Add
    
    wbNext = Dir(wbPath & Extn)
    Do While wbNext <> ""
        'open next workbook allowing time to complete that event
            Set wb = Workbooks.Open(wbPath & wbNext)
            DoEvents
        'add first sheet sheet to temporary workbook
            wb.Sheets(1).Copy after:=wbTEMP.Sheets(wbTEMP.Sheets.Count)
            DoEvents
        'export individual workbook to PDF
            pdfName = Left(wbNext, InStrRev(wbNext, ".") - 1) & ".pdf"
            wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath & pdfName
            wb.Close SaveChanges:=False
            DoEvents
        wbNext = Dir
    Loop
        'export temporary workbook to pdf and close without saving
            Application.DisplayAlerts = False
            wbTEMP.Sheets(1).Delete
            Application.DisplayAlerts = True
            wbTEMP.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath & "Merged " & Format(Now, "YY-MM-DD HHMM")
            wbTEMP.Close False
End Sub
 
Upvote 0
Amended procedure as requested

VBA Code:
Sub CreatePdf()
    Application.ScreenUpdating = False
    Dim Extn As String, wbNext As String, pdfName As String, wbTEMP As Workbook, wb As Workbook
    Const wbPath = "C:\TestArea\A1\"
    Const pdfPath = "C:\TestArea\A2\"
    Extn = "*.xls*"
    Set wbTEMP = Workbooks.Add
   
    wbNext = Dir(wbPath & Extn)
    Do While wbNext <> ""
        'open next workbook allowing time to complete that event
            Set wb = Workbooks.Open(wbPath & wbNext)
            DoEvents
        'add first sheet sheet to temporary workbook
            wb.Sheets(1).Copy after:=wbTEMP.Sheets(wbTEMP.Sheets.Count)
            DoEvents
        'export individual workbook to PDF
            pdfName = Left(wbNext, InStrRev(wbNext, ".") - 1) & ".pdf"
            wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath & pdfName
            wb.Close SaveChanges:=False
            DoEvents
        wbNext = Dir
    Loop
        'export temporary workbook to pdf and close without saving
            Application.DisplayAlerts = False
            wbTEMP.Sheets(1).Delete
            Application.DisplayAlerts = True
            wbTEMP.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath & "Merged " & Format(Now, "YY-MM-DD HHMM")
            wbTEMP.Close False
End Sub
Thanks for the help. However, I received an error Run time 1004 - The name is already taken. Try a different one.
 
Upvote 0
Tell me what you want to happen

Perhaps guarantee unique name with something like this ...
pdfName = Left(wbNext, InStrRev(wbNext, ".") - 1) & Format(Now, "YY-MM-DD HHMM") & ".pdf"

or replace the file if it already exists

or stop the code with a meassage

or something else
 
Upvote 0
Tell me what you want to happen

Perhaps guarantee unique name with something like this ...
pdfName = Left(wbNext, InStrRev(wbNext, ".") - 1) & Format(Now, "YY-MM-DD HHMM") & ".pdf"

or replace the file if it already exists

or stop the code with a meassage

or something else
I tried the code but I'm stilling getting the same error - pdfName = Left(wbNext, InStrRev(wbNext, ".") - 1) & Format(Now, "YY-MM-DD HHMM") & ".pdf"

I have the number below and noticed that after doing the first run, it skips number "2. " and work on "10. "

1. Name
2. Name
...
10.

Also, I'm unsure if the error has to do with the temp workbook...after Book 1 is closed the temp workbook is named as Book 2 and so on.

Any thoughts will be much appreciated. :)
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,643
Members
452,663
Latest member
MEMEH

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