Workbooks in a folder to PDF

Aggie2014

New Member
Joined
Dec 26, 2019
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I'm working on a small process improvement project at work and am running into a wall. The goal is to execute a macro that will take the first sheet of each workbook within a folder and print them as PDF files with the same filename as the original file, with the different extension.

I found some starting code on Stackoverflow that I adjusted for my needs, but when I run it, it gives me an error that it cannot find the first Excel file in the directory, even though the files are there. It's find the name of the file, but for some reason isn't able to capture it and open it.

I posted the code below. Any guidance would be appreciated!

Option Explicit



VBA Code:
Option Explicit

 

Sub Excel_files_to_PDF()

    Dim strWorkbook As String

    Dim wbktoExport As Workbook

    Dim wbkLocation As String

  

    wbkLocation = "C:\Users\userName\Desktop\New folder\"

  

    strWorkbook = Dir(wbkLocation & "*.xls*")

  

    Do While Len(strWorkbook) > 0

    wbktoExport = Workbooks.Open(strWorkbook)

 
    Call Save_to_PDF(wbktoExport)

 
    strWorkbook = Dir


    wbktoExport.Close False


    Loop

End Sub

 
Sub Save_to_PDF(ByRef wbk As Workbook)

    Dim strTargetPDFLocation As String

    strTargetPDFLocation = "C:\Users\userName\Desktop\New folder\"

    wbk.Sheets(1).Select

    wbk.Sheets(1).Activate

  

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTargetPDFLocation & wbk.Name & ".pdf", _

    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

  

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
wbktoExport = Workbooks.Open(strWorkbook)
A common mistake - Dir returns just the file name, without the folder path. You also need to Set the Workbook object:
VBA Code:
Set wbktoExport = Workbooks.Open(wbkLocation & strWorkbook)
 
Upvote 0
That worked perfectly. Thanks!

Another issue I'm running into is that on some of the workbooks there are links and when they're opened, Excel alerts the "We can't update some of the links in the workbook right now" message. Is there a way to set a parameter within the VBA to continue automatically?
 
Upvote 0
Try inserting Application.DisplayAlerts = False before the loop and Application.DisplayAlerts = True after it.
 
Upvote 0
Perfection. Really appreciate the help! This will save us hours of work..
 
Upvote 0

Forum statistics

Threads
1,214,563
Messages
6,120,248
Members
448,952
Latest member
kjurney

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