VBA - Export individual PowerPoint slides into PDF

2019-08-27 21:49发布

问题:

I have about 15 different slide decks, that contain 3-4 slides each. Each deck is comprised of "certificates" to be broken out and given to each individual recipient, who is named on the certificate. This process is repeated every month.

ideally, I would like to enable a VBA script to extract each sheet to save as (or print as) a PDF, and save the file with the name of the recipient named on the slide... is this possible?

The best I have found is this code, (which doesn't work on my mac, but works fine on a VM) which DOES separate the slides into PDF's, but I would like the naming to be changed.

Any help would be greatly appreciated!!

Sub ExportSlidesToIndividualPDF()
Dim oPPT As Presentation, oSlide As Slide
Dim sPath As String, sExt As String

Set oPPT = ActivePresentation
sPath = oPPT.FullName & "_Slide_"
sExt = ".pdf"

For Each oSlide In oPPT.Slides
    i = oSlide.SlideNumber
    oSlide.Select
    oPPT.ExportAsFixedFormat _
        Path:=sPath & i & sExt, _
        FixedFormatType:=ppFixedFormatTypePDF, _
        RangeType:=ppPrintSelection
Next
Set oPPT = Nothing
End Sub

Edit for further clarification:

I'm forced to run the script in my Windows Virtual Machine, and when I do that, I'm losing all of my formatting that I set up in my slides, created in office '11 (which is one of my big reasons to export to PDF in the first place).

A little more about my project:
1. I am compiling a couple thousand lines of "actions" (data), to determine a winner for a recognition award, based on criteria for 4 different award categories, for 12-15 different executives.
2. Results are tallied, then I retrieve the winners information (name, ID, and the same for their manager and executive) from an LDAP directory. Then put into a csv for mail-merge at later step.
3. A Template slide deck is used for all slides, (12-15 executives have 3-4 winners each).
4. Individual PDF certificates are created from those slides.
5. Congratulatory message is drafted, fields pulled from prepared csv for mail-merge, and then exported to outlook 2011 outbox (client offline).
6. I attach each individual certificate to each message manually in the outbox.
7. Send.

I have compiled sample set of the files linked here: Recognition Files (Sample)

回答1:

On each slide there are one or several shapes. For the sake of illustration, assume that the recipient's name is in the first shape.

Sub ExportSlidesToIndividualPDF()
Dim oPPT As Presentation, oSlide As Slide
Dim sPath As String, sExt As String

Set oPPT = ActivePresentation

sExt = ".pdf"

For Each oSlide In oPPT.Slides
    '## Retrieve the recipient's name from the shape and append it to the exported path:
    sPath = oPPT.FullName & oSlide.Shapes(1).TextFrame.TextRange.Text
    i = oSlide.SlideNumber
    oSlide.Select
    oPPT.ExportAsFixedFormat _
        Path:=sPath & sExt, _
        FixedFormatType:=ppFixedFormatTypePDF, _
        RangeType:=ppPrintSelection
Next
Set oPPT = Nothing
End Sub

It may be more complicated, depending on how the slides are configured (e.g., if there is a shape that contains the name and other text, you will need a function to parse out the proper name and omit the rest, etc.

Provide more detail, about the problem if you need further assistance.