Using COM for PowerPoint

Post your working scripts, libraries and tools for AHK v1.1 and older
julesverne
Posts: 42
Joined: 18 Apr 2017, 14:39
Contact:

Using COM for PowerPoint

16 Apr 2018, 17:04

I took some time this weekend to try and demystify COM a little by rewriting a working vbscript for PowerPoint into ahk. I know I'm not the only one that freaks out a little when it comes to COM however I am in love with the advantages of it.

First I'll give the widely used internet vbscript (.vbs) by @BillP3rd that exports PowerPoint to PDF. This script is different from .SaveAs method as it allows options to PDF rather than just a strict page by page PDF. For example you can have the PDF output show 1,2,3,4,5,6 slides per PDF page. If you just want to do a simple SaveAs to PDF with no options you can find the ahk code here: https://autohotkey.com/boards/viewtopic ... PowerPoint

You can access the different options by paying attention to the Constants (Const) in the vbscript. Constants are simply variables that don't change, but their values are numbers, and so it's easier to use variables for human eyes. The script below creates a PDF of 2 slides per page of all the pages.

To use this code, save the file as ppttopdf.vbs. open up a command prompt and type in cscript //nologo %pathtofolder%\ppttopdf.vbs "%pathandfilenameOfPPTfile%" "%pathtosavetoandfilenametoSaveAsPDF%" replace pathtofolder and pathandfilenameOfPPTfile pathtosavetoandfilenametoSaveAsPDFto your file.
ex. cscript //nologo C:\Test\ppttopdf.vbs "C:\Test\file.PPTX" "C:\Test\file.pdf"

Code: Select all

' Courtesy BillP3rd of superuser.com

Option Explicit

Sub WriteLine ( strLine )
    WScript.Stdout.WriteLine strLine
End Sub

' http://msdn.microsoft.com/en-us/library/office/aa432714(v=office.12).aspx
Const msoFalse = 0   ' False.
Const msoTrue = -1   ' True.

' http://msdn.microsoft.com/en-us/library/office/bb265636(v=office.12).aspx
Const ppFixedFormatIntentScreen = 1 ' Intent is to view exported file on screen.
Const ppFixedFormatIntentPrint = 2  ' Intent is to print exported file.

' http://msdn.microsoft.com/en-us/library/office/ff746754.aspx
Const ppFixedFormatTypeXPS = 1  ' XPS format
Const ppFixedFormatTypePDF = 2  ' PDF format

' http://msdn.microsoft.com/en-us/library/office/ff744564.aspx
Const ppPrintHandoutVerticalFirst = 1   ' Slides are ordered vertically, with the first slide in the upper-left corner and the second slide below it.
Const ppPrintHandoutHorizontalFirst = 2 ' Slides are ordered horizontally, with the first slide in the upper-left corner and the second slide to the right of it.

' http://msdn.microsoft.com/en-us/library/office/ff744185.aspx
Const ppPrintOutputSlides = 1               ' Slides
Const ppPrintOutputTwoSlideHandouts = 2     ' Two Slide Handouts
Const ppPrintOutputThreeSlideHandouts = 3   ' Three Slide Handouts
Const ppPrintOutputSixSlideHandouts = 4     ' Six Slide Handouts
Const ppPrintOutputNotesPages = 5           ' Notes Pages
Const ppPrintOutputOutline = 6              ' Outline
Const ppPrintOutputBuildSlides = 7          ' Build Slides
Const ppPrintOutputFourSlideHandouts = 8    ' Four Slide Handouts
Const ppPrintOutputNineSlideHandouts = 9    ' Nine Slide Handouts
Const ppPrintOutputOneSlideHandouts = 10    ' Single Slide Handouts

' http://msdn.microsoft.com/en-us/library/office/ff745585.aspx
Const ppPrintAll = 1            ' Print all slides in the presentation.
Const ppPrintSelection = 2      ' Print a selection of slides.
Const ppPrintCurrent = 3        ' Print the current slide from the presentation.
Const ppPrintSlideRange = 4     ' Print a range of slides.
Const ppPrintNamedSlideShow = 5 ' Print a named slideshow.

' http://msdn.microsoft.com/en-us/library/office/ff744228.aspx
Const ppShowAll = 1             ' Show all.
Const ppShowNamedSlideShow = 3  ' Show named slideshow.
Const ppShowSlideRange = 2      ' Show slide range.

'
' This is the actual script
'

Dim inputFile
Dim outputFile
Dim objPPT
Dim objPresentation
Dim objPrintOptions
Dim objFso

If WScript.Arguments.Count <> 2 Then
    WriteLine "You need to specify input and output files."
    WScript.Quit
End If
inputFile = WScript.Arguments(0)
outputFile = WScript.Arguments(1)

Set objFso = CreateObject("Scripting.FileSystemObject")

If Not objFso.FileExists( inputFile ) Then
    WriteLine "Unable to find your input file " & inputFile
    WScript.Quit
End If

If objFso.FileExists( outputFile ) Then
    'WriteLine "Your output file (' & outputFile & ') already exists!"
    'WScript.Quit
End If

WriteLine "Input File:  " & inputFile
WriteLine "Output File: " & outputFile

Set objPPT = CreateObject( "PowerPoint.Application" )


objPPT.Presentations.Open inputFile
Set objPresentation = objPPT.ActivePresentation
Set objPrintOptions = objPresentation.PrintOptions

objPrintOptions.Ranges.Add 1,objPresentation.Slides.Count
objPrintOptions.RangeType = ppShowAll

' Reference for this at http://msdn.microsoft.com/en-us/library/office/ff746080.aspx
objPresentation.ExportAsFixedFormat outputFile, ppFixedFormatTypePDF, ppFixedFormatIntentScreen, msoFalse, ppPrintHandoutVerticalFirst, ppPrintOutputTwoSlideHandouts, msoFalse, objPrintOptions.Ranges(1), ppPrintAll, "Slideshow Name", False, False, False, False, False

objPresentation.Close
ObjPPT.Quit
Things to keep in mind about the difference between ahk and vb(s):
  1. 1.)ahk does not use Const variables, however in my research @kon on this board came up with a Microsoft Office Const library here: https://autohotkey.com/boards/viewtopic ... nt#p108740 I have not used it so I can't provide insight on it. But for the purposes of this script we won't need it.
  • 2.)ahk does not Dim variables. Dim is just declaring variables. vbscript needs to know what variables you will use, if you want to use them globally in different subroutines, functions, etc.
  • 3.)option explicit in vbscript is related to the Dim command so again, is unnecessary in the ahk script. If there is an equivalent to it in ahk, I do not know what it is.
So below is the ahk script. The ahk code itself is written below the vbs code (commented out) and some additional (also commented out) so you can get a good visual as to how to rewrite vbscript to COM for ahk. It's pretty similar code in some circumstances. One thing to keep in mind is that COM code is written as expressions in ahk.

Run this script like the vbscript. Save this script as PPTtoPDF.ahk %pathtofolder%\PPTtoPDF.ahk "%pathandfilenameOfPPTfile%" "%pathtosavetoandfilenametoSaveAsPDF%" replace pathtofolder and pathandfilenameOfPPTfile pathtosavetoandfilenametoSaveAsPDFto your file.
ex. C:\Test\PPTtoPDF.ahk "C:\Test\file.PPTX" "C:\Test\file.pdf"

Code: Select all

;Option Explicit
;no ahk equivalent and not necessary in ahk

;Sub WriteLine ( strLine )
;    WScript.Stdout.WriteLine strLine
;End Sub
; this sub writes messages below to the command line.
; in ahk this is a bit tricky so I am going to use msgbox instead below

; the next section are the constants. You don't need all of the variables in your script. You can just use the variables you want so you don't have so much excess code.
; However, this is a good reference if you want to know your different PDF output options.

;' http://msdn.microsoft.com/en-us/library/office/aa432714(v=office.12).aspx
;Const msoFalse = 0   ' False.
;Const msoTrue = -1   ' True.
; these are just true or false variables
msoFalse := 0 ;False.
msoTrue := 1 ; True.

;' http://msdn.microsoft.com/en-us/library/office/bb265636(v=office.12).aspx
;Const ppFixedFormatIntentScreen = 1 ' Intent is to view exported file on screen.
;Const ppFixedFormatIntentPrint = 2  ' Intent is to print exported file.
ppFixedFormatIntentScreen := 1 ; Intent is to view exported file on screen.
ppFixedFormatIntentPrint := 2 ; Intent is to print exported file.

;' http://msdn.microsoft.com/en-us/library/office/ff746754.aspx
;Const ppFixedFormatTypeXPS = 1  ' XPS format
;Const ppFixedFormatTypePDF = 2  ' PDF format
ppFixedFormatTypeXPS := 1 ; XPS format
ppFixedFormatTypePDF := 2 ; PDF format

;' http://msdn.microsoft.com/en-us/library/office/ff744564.aspx
;Const ppPrintHandoutVerticalFirst = 1   ' Slides are ordered vertically, with the first slide in the upper-left corner and the second slide below it.
;Const ppPrintHandoutHorizontalFirst = 2 ' Slides are ordered horizontally, with the first slide in the upper-left corner and the second slide to the right of it.
ppPrintHandoutVerticalFirst := 1 ; Slides are ordered vertically, with the first slide in the upper-left corner and the second slide below it.
ppPrintHandoutHorizontalFirst := 2 ; Slides are ordered horizontally, with the first slide in the upper-left corner and the second slide to the right of it.

;' http://msdn.microsoft.com/en-us/library/office/ff744185.aspx
;Const ppPrintOutputSlides = 1               ' Slides
;Const ppPrintOutputTwoSlideHandouts = 2     ' Two Slide Handouts
;Const ppPrintOutputThreeSlideHandouts = 3   ' Three Slide Handouts
;Const ppPrintOutputSixSlideHandouts = 4     ' Six Slide Handouts
;Const ppPrintOutputNotesPages = 5           ' Notes Pages
;Const ppPrintOutputOutline = 6              ' Outline
;Const ppPrintOutputBuildSlides = 7          ' Build Slides
;Const ppPrintOutputFourSlideHandouts = 8    ' Four Slide Handouts
;Const ppPrintOutputNineSlideHandouts = 9    ' Nine Slide Handouts
;Const ppPrintOutputOneSlideHandouts = 10    ' Single Slide Handouts
ppPrintOutputSlides := 1 ; Slides
ppPrintOutputTwoSlideHandouts := 2 ; Two Slide Handouts
ppPrintOutputThreeSlideHandouts := 3 ;Three Slide Handouts
ppPrintOutputSixSlideHandouts := 4 ; Six Slide Handouts
ppPrintOutputNotesPages := 5 ; Notes Pages
ppPrintOutputOutline := 6 ; Outline
ppPrintOutputBuildSlides := 7 ; Build Slides
ppPrintOutputFourSlideHandouts := 8 ; Four Slide Handouts
ppPrintOutputNineSlideHandouts := 9 ; Nine Slide Handouts
ppPrintOutputOneSlideHandouts := 10 ; Single Slide Handouts

;' http://msdn.microsoft.com/en-us/library/office/ff745585.aspx
;Const ppPrintAll = 1            ' Print all slides in the presentation.
;Const ppPrintSelection = 2      ' Print a selection of slides.
;Const ppPrintCurrent = 3        ' Print the current slide from the presentation.
;Const ppPrintSlideRange = 4     ' Print a range of slides.
;Const ppPrintNamedSlideShow = 5 ' Print a named slideshow.
ppPrintAll := 1            ; Print all slides in the presentation.
ppPrintSelection := 2      ; Print a selection of slides.
ppPrintCurrent := 3        ; Print the current slide from the presentation.
ppPrintSlideRange := 4     ; Print a range of slides.
ppPrintNamedSlideShow := 5 ; Print a named slideshow.

;' http://msdn.microsoft.com/en-us/library/office/ff744228.aspx
;Const ppShowAll = 1             ' Show all.
;Const ppShowNamedSlideShow = 3  ' Show named slideshow.
;Const ppShowSlideRange = 2      ' Show slide range.
ppShowAll = 1             ; Show all.
ppShowNamedSlideShow = 3  ; Show named slideshow.
ppShowSlideRange = 2      ; Show slide range.

;'
;' This is the actual script
;'

;Dim inputFile
;Dim outputFile
;Dim objPPT
;Dim objPresentation
;Dim objPrintOptions
;Dim objFso
;Dim statements are unnecessary in ahk.

;If WScript.Arguments.Count <> 2 Then
;    WriteLine "You need to specify input and output files."
;    WScript.Quit
;End If
;inputFile = WScript.Arguments(0)
;outputFile = WScript.Arguments(1)
args = %0%
if (args > 2)
{
	msgbox, You need to specify input and output files.  Exiting.
	ExitApp
}
inputFile = %1%
outputFile = %2%

; Set objFso = CreateObject("Scripting.FileSystemObject")
; not necessary in ahk

;If Not objFso.FileExists( inputFile ) Then
;    WriteLine "Unable to find your input file " & inputFile
;    WScript.Quit
;End If
IfNotExist, %inputFile%
{
	msgbox, Unable to find your input file %inputFile% .. Exiting.
	ExitApp
}

;If objFso.FileExists( outputFile ) Then
;   'WriteLine "Your output file (' & outputFile & ') already exists!"
;    'WScript.Quit
;End If
IfExist, %outputFile%
{
	Msgbox, Your output file %outputFile% already exists! .. Exiting
	ExitApp
}

;WriteLine "Input File:  " & inputFile
;WriteLine "Output File: " & outputFile
msgbox, Input File: %inputFile%
msgbox, Output File: %outputFile%

;Set objPPT = CreateObject( "PowerPoint.Application" )
ObjPPT := ComObjCreate("PowerPoint.Application")

;objPPT.Presentations.Open inputFile
ObjPPT.Presentations.Open(inputFile)

;Set objPresentation = objPPT.ActivePresentation
objPresentation := ObjPPT.ActivePresentation

;Set objPrintOptions = objPresentation.PrintOptions
objPrintOptions := objPresentation.PrintOptions

;objPrintOptions.Ranges.Add 1,objPresentation.Slides.Count
;this vb line above says I want to print slides 1 to the last slide.
; a clever way to get the last slide number of the presentation by counting all the slides
; the objPresentation.Slides.Count can be written as a separate expression if it makes things easier to understand
; it would look like this LastSlideofPresentation := objPresentation.Slides.Count
; then the line below would be objPrintOptions.Ranges.Add(1, LastSlideofPresentation)
objPrintOptions.Ranges.Add(1, objPresentation.Slides.Count)

;objPrintOptions.RangeType = ppShowAll
objPrintOptions.RangeType := ppShowAll

;' Reference for this at http://msdn.microsoft.com/en-us/library/office/ff746080.aspx
;objPresentation.ExportAsFixedFormat outputFile, ppFixedFormatTypePDF, ppFixedFormatIntentScreen, msoFalse, ppPrintHandoutVerticalFirst, ppPrintOutputTwoSlideHandouts, msoFalse, objPrintOptions.Ranges(1), ppPrintAll, "Slideshow Name", False, False, False, False, False
objPresentation.ExportAsFixedFormat( outputFile, ppFixedFormatTypePDF, ppFixedFormatIntentScreen, msoFalse, ppPrintHandoutVerticalFirst, ppPrintOutputTwoSlideHandouts, msoFalse, objPrintOptions.Ranges(1), ppPrintAll, "Slideshow Name", False, False, False, False, False)

;objPresentation.Close
objPresentation.Close
;ObjPPT.Quit
ObjPPT.Quit

; below is not in vbscript but is needed in ahk to fully close out of PowerPoint. 
ObjPPT := ""
Please feel free to comment. Make improvements. Suggestions. Questions. Hope this is helpful to someone.
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: Using COM for PowerPoint

22 Jan 2021, 12:34

This is very helpful. Thank you!
julesverne
Posts: 42
Joined: 18 Apr 2017, 14:39
Contact:

Re: Using COM for PowerPoint

22 Jan 2021, 13:35

Glad to hear it @SirSocks ! I've learned a lot more since this simple tutorial but we all gotta start somewhere.
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Using COM for PowerPoint

23 Jan 2021, 19:49

@julesverne, thanks for this. You might have a look at this thread for a function to work with an existing PowerPoint COM object rather than creating one, very much like the Excel_Get function you can find in the forum.
Regards,
burque505

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: ositoMalvado, RussF and 119 guests