Skip to content

Instantly share code, notes, and snippets.

@ap0llo
Created January 11, 2017 20:25
Show Gist options
  • Save ap0llo/5c5f5aadb885fe918000b248e5dd6e36 to your computer and use it in GitHub Desktop.
Save ap0llo/5c5f5aadb885fe918000b248e5dd6e36 to your computer and use it in GitHub Desktop.
# Powershell script to export Powerpoint slides as jpg images using the Powerpoint COM API
# Powershell script to export Powerpoint slides as jpg images using the Powerpoint COM API
function Export-Slide($inputFile, $slideNumber, $outputFile)
{
# Load Powerpoint Interop Assembly
[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Powerpoint") > $null
[Reflection.Assembly]::LoadWithPartialname("Office") > $null
$msoFalse = [Microsoft.Office.Core.MsoTristate]::msoFalse
$msoTrue = [Microsoft.Office.Core.MsoTristate]::msoTrue
# start Powerpoint
$application = New-Object "Microsoft.Office.Interop.Powerpoint.ApplicationClass"
# Make sure inputFile is an absolte path
$inputFile = Resolve-Path $inputFile
$presentation = $application.Presentations.Open($inputFile, $msoTrue, $msoFalse, $msoFalse)
$slide = $presentation.Slides.Item($slideNumber)
$slide.Export($outputFile, "JPG")
$slide = $null
$presentation.Close()
$presentation = $null
if($application.Windows.Count -eq 0)
{
$application.Quit()
}
$application = $null
# Make sure references to COM objects are released, otherwise powerpoint might not close
# (calling the methods twice is intentional, see https://msdn.microsoft.com/en-us/library/aa679807(office.11).aspx#officeinteroperabilitych2_part2_gc)
[System.GC]::Collect();
[System.GC]::WaitForPendingFinalizers();
[System.GC]::Collect();
[System.GC]::WaitForPendingFinalizers();
}
@CraigChamberlain
Copy link

CraigChamberlain commented Nov 7, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment