Menu

Export Powerpoint file as PDF

Help
Dirk Weil
2014-03-20
2023-05-31
  • Dirk Weil

    Dirk Weil - 2014-03-20

    I would like to export a PowerPoint presentation as PDF. It work by using the presetation method SaveAs:

    comPowerpoint = new ActiveXComponent("PowerPoint.Application");
    Dispatch comPresentations = Dispatch.get(comPowerpoint, "Presentations").toDispatch();

    Dispatch comPresentation = Dispatch.call(comPresentations, "Open", "D:\temp\dummy.pptx").toDispatch();
    Dispatch.call(comPresentation, "SaveAs", "D:\temp\dummy3.pdf", 32);

    The magic number 32 selects PDF as output format.

    But that would export the slides only. In order to export the notes, I tried to use the method ExportAsFixedFormat instead:

    Dispatch.call(comPresentation, "ExportAsFixedFormat", "D:\temp\dummy3.pdf", 2);

    (For notes instead of slides more parameters are necessary).

    That call ends in an exception thrown:

    com.jacob.com.ComFailException: A COM exception has been encountered:
    At Invoke of: ExportAsFixedFormat
    Description: 80020005 / Typkonflikt.

    By trial and error I discovered that the magic number 2 seems to be the reason. But what do I have to supply as third parameter for selecting PDF?

    Thanks in advance
    Dirk

     
  • René Jahn

    René Jahn - 2023-05-31

    Long time ago, but still the same problem!

    The problem is not the last parameter: 2.
    It's the missing PrintRange parameter. The parameter is optional, but check this:
    https://sourceforge.net/p/pywin32/bugs/339/
    (It defines "[in, optional, defaultvalue(0)] PrintRange* PrintRange" which leads to the generation of "PrintRange=0")

    Same problem for jacob. I had the same problem and solved it like this:

    Dispatch printOptions = Dispatch.get(presentation, "PrintOptions").toDispatch();
    Dispatch ranges = Dispatch.get(printOptions, "Ranges").toDispatch();
    Dispatch.call(ranges, "ClearAll");
    Dispatch slides = Dispatch.get(presentation, "Slides").toDispatch();
    Dispatch.call(ranges, "Add", Integer.valueOf(1), Integer.valueOf(1)).toDispatch();                  
    Dispatch range = Dispatch.call(ranges, "Item", Integer.valueOf(1)).toDispatch();
    
     //the range index is not important because the last parameter exports all slides                   
    Dispatch.call(presentation, "ExportAsFixedFormat", pPdf.getAbsolutePath(), 
                                                                           Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(0), 
                                                                           Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0), 
                                                                           range, Integer.valueOf(1));
    

    Above code is not production ready because empty presentations are not checked, but this shouldn't be a big problem.

     

Log in to post a comment.