+ Reply to Thread
Results 1 to 13 of 13

VBA to convert multiple PowerPoint files into one pdf

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    VBA to convert multiple PowerPoint files into one pdf

    Hello,

    From an excel workbook, I want to select a folder (with filepath referenced in cell k3) containing approx 20 PowerPoint presentations and export their content into a pdf file with the same name as the original but with the new .pdf suffix. I found some vba online which I have tried to adapt but it gives an error on the line which is removing the file extensions. Please would someone be willing to take a look and advise?

    This is the line which is currently giving the error but I don�t know if this is the only problem;

    removeFileExt = InStr(1, oPPTFile.Name, ".") - 1
    Thank you,

    Jess

    Sub ppt2pdf()
    Dim oPPTApp As PowerPoint.Application
    Dim oPPTFile As PowerPoint.Presentation
    Dim onlyFileName As String, folderpath As String, pptFiles As String, removeFileExt As Long
     
    Application.ScreenUpdating = False
     
    'initialize variables
    folderpath = Range("k3").Text & "\"
    'pptFiles = Dir(folderpath & "*.pp*") (Original)
    pptFiles = (folderpath & "*.pp*")
     
    'check for ppt files and exit if not found
    If pptFiles = "" Then
        MsgBox "No files found"
        Exit Sub
    End If
     
    Do While pptFiles <> ""
     
    'Assign powerpoint application to variable
    Set oPPTApp = CreateObject("PowerPoint.Application")
    oPPTApp.Visible = msoTrue
     
    On Error Resume Next
     
    'Assign powerpoint presentation to variable
    Set oPPTFile = oPPTApp.Presentations.Open(folderpath & pptFiles)
     
    On Error GoTo 0
     
    'remove file extension and assign an only file name to a variable
    removeFileExt = InStr(1, oPPTFile.Name, ".") - 1
     
    onlyFileName = Left(oPPTFile.Name, removeFileExt)
     
    On Error Resume Next
     
    'Save ppt file to pdf file
    oPPTFile.ExportAsFixedFormat oPPTFile.Path & "\" & onlyFileName & ".pdf", ppFixedFormatTypePDF, ppFixedFormatIntentPrint
    oPPTFile.Close
     
    'iterate to the next file in the folder
    pptFiles = Dir()
    Loop
     
    'Close PPT application
    oPPTApp.Quit
    Set oPPTFile = Nothing
    Set oPPTApp = Nothing
     
    Application.ScreenUpdating = True
     
    End Sub
    Last edited by Brontosaurus; 01-15-2025 at 08:54 AM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,552

    Re: VBA to convert multiple PowerPoint files into one pdf

    Replace this
    'remove file extension and assign an only file name to a variable
    removeFileExt = InStr(1, oPPTFile.Name, ".") - 1
     
    onlyFileName = Left(oPPTFile.Name, removeFileExt)
    by
    onlyFileName = CreateObject("scripting.filesystemobject").getbasename(oPPTFile.Name)
    Also, why did you change this.
    'pptFiles = Dir(folderpath & "*.pp*") (Original)
    pptFiles = (folderpath & "*.pp*")
    My best guess is that by doing so there are no files to proccess
    Last edited by bakerman2; 01-14-2025 at 12:08 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    Re: VBA to convert multiple PowerPoint files into one pdf

    Hi, thank you for your suggestion.

    I tried the CreateObject code which results in an error 91 - object variable or with block variable not set.

    And I changed the pptFiles code because I got a runtime error 52 - bad file name or number.

    Please would you offer further suggestions?

    Thank you

    'pptFiles = Dir(folderpath & "*.pp*") (Original)
    pptFiles = (folderpath & "*.pp*")

  4. #4
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    Re: VBA to convert multiple PowerPoint files into one pdf

    Here’s the full code following the changes being introduced;

    
    Sub ppt2pdf()
    Dim oPPTApp As PowerPoint.Application
    Dim oPPTFile As PowerPoint.Presentation
    Dim onlyFileName As String, folderpath As String, pptFiles As String, removeFileExt As Long
     
    Application.ScreenUpdating = False
     
    'initialize variables
    folderpath = Range("k3").Text & "\"
    pptFiles = Dir(folderpath & "*.pp*")
    ‘pptFiles = (folderpath & "*.pp*")
     
    'check for ppt files and exit if not found
    If pptFiles = "" Then
        MsgBox "No files found"
        Exit Sub
    End If
     
    Do While pptFiles <> ""
     
    'Assign powerpoint application to variable
    Set oPPTApp = CreateObject("PowerPoint.Application")
    oPPTApp.Visible = msoTrue
     
    On Error Resume Next
     
    'Assign powerpoint presentation to variable
    Set oPPTFile = oPPTApp.Presentations.Open(folderpath & pptFiles)
     
    On Error GoTo 0
     
    'remove file extension and assign an only file name to a variable
    'removeFileExt = InStr(1, oPPTFile.Name, ".") - 1
    'onlyFileName = Left(oPPTFile.Name, removeFileExt)
     
    onlyFileName = CreateObject("scripting.filesystemobject").getbasename(oPPTFile.Name)
     
    On Error Resume Next
     
    'Save ppt file to pdf file
    oPPTFile.ExportAsFixedFormat oPPTFile.Path & "\" & onlyFileName & ".pdf", ppFixedFormatTypePDF, ppFixedFormatIntentPrint
    oPPTFile.Close
     
    'iterate to the next file in the folder
    pptFiles = Dir()
    Loop
     
    'Close PPT application
    oPPTApp.Quit
    Set oPPTFile = Nothing
    Set oPPTApp = Nothing
     
    Application.ScreenUpdating = True
     
    End Sub

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,552

    Re: VBA to convert multiple PowerPoint files into one pdf

    This worked fine for me.
    Sub ppt2pdf()
        Dim Path As String, FilesInPath As String, MyFiles() As String, Fnum As Long
        
        Application.ScreenUpdating = False
        Path = Sheets("Sheet1").Range("K3").Text & "\"
        FilesInPath = Dir(Path & "*.pp*")
        If FilesInPath = "" Then
            MsgBox "No files found"
            Exit Sub
        End If
        Fnum = 0
        Do While FilesInPath <> ""
            Fnum = Fnum + 1
            ReDim Preserve MyFiles(1 To Fnum)
            MyFiles(Fnum) = FilesInPath
            FilesInPath = Dir()
        Loop
        If Fnum > 0 Then
            For Fnum = LBound(MyFiles) To UBound(MyFiles)
                With CreateObject("PowerPoint.Application")
                    .Visible = msoTrue
                    With .Presentations.Open(Path & MyFiles(Fnum))
                        onlyFileName = CreateObject("scripting.filesystemobject").getbasename(MyFiles(Fnum))
                        .SaveAs Path & onlyFileName & ".pdf", 32
                        .Close
                    End With
                End With
            Next Fnum
        End If
    End Sub

  6. #6
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    Re: VBA to convert multiple PowerPoint files into one pdf

    Good morning and thank you again for helping with this.

    I still get the error (52 - bad file name or number) with this line;

    FilesInPath = Dir(Path & "*.pp*")
    Please would you offer a suggestion on how to correct it?

    Thank you

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,552

    Re: VBA to convert multiple PowerPoint files into one pdf

    What do your filenames look like because as I said for me it works just fine.
    Is the filepath in K3 correct ? Is the sheetname correct ?

  8. #8
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    Re: VBA to convert multiple PowerPoint files into one pdf

    There is one file with the name testppt.pptx.
    I believe that the filepath in K3 is correct; it is in a Teams location.
    The sheet name is “instructions” which I’ve changed in the code

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,552

    Re: VBA to convert multiple PowerPoint files into one pdf

    I don't have experience with that but for testing purposes can you move some files to a folder on a local drive,
    change the path in the sheet and try from there.

  10. #10
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    Re: VBA to convert multiple PowerPoint files into one pdf

    Hi, I’ve done that and it works perfectly thank you. The problem seems to be related to the fact that the folder is located in Teams

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,552

    Re: VBA to convert multiple PowerPoint files into one pdf

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon (Next to Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  12. #12
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    Re: VBA to convert multiple PowerPoint files into one pdf

    Hi, I’ve now tested a workaround by referring to the Teams file location as a local C drive location and it works so fully solved and I sincerely appreciate your help. Thank you so much, Jess

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,552

    Re: VBA to convert multiple PowerPoint files into one pdf

    You're welcome and really glad it all turned out well.
    Thanks for Rep+.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to print multiple Powerpoint files using Macro
    By greenarrow in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-31-2017, 11:08 AM
  2. convert multiple pptx files into mp4 videos using powerpoint vba
    By shiva_raj in forum PowerPoint Formatting & General
    Replies: 3
    Last Post: 04-03-2017, 02:25 AM
  3. Windows 7 :Need to convert PDF files to PowerPoint,Any suggestion?
    By hourer in forum Microsoft Windows Help
    Replies: 6
    Last Post: 03-11-2016, 06:30 AM
  4. [SOLVED] Convert all Powerpoint files into PDF from an Excel VBA
    By benishiryo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2016, 10:25 AM
  5. Convert Multiple text files to excel files
    By shanmugapriya.a in forum Excel General
    Replies: 3
    Last Post: 07-07-2015, 10:39 AM
  6. Convert multiple text files to be excel files
    By PStump in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-24-2014, 05:44 PM
  7. [SOLVED] how to convert multiple .txt files to .xlsx files using a macro?
    By Aaron_Tram in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-21-2013, 06:30 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1