Text searching a Powerpoint document for a string

by Sep 8, 2011

I haven't found any good examples of how to text search a powerpoint document.    I have something like this working for word and excel documents but having a problem with shapes in powerpoint.

I believe I must find the number of slides and walk them… then within that determine the number of shapes on each slide and walk them.  On shapes that are of type text I need to search for a string.

I am stuck on determining the number of shapes on a slide.   I can put the .ppt and .pptx files in a collection, put the slides for each file in a collection and walk it, but I have a problem figuring out the number of shapes and putting them in a collection to walk.  

My goal is to find the number of shapes of type text and then search it for a string.   

I am using office (powerpoint) 2007, & Win7 x64.  

Any help getting past the shapes problem would be most appreciated.

This is what I have pieced together so far from using other examples I have seen…

# Search Powerpoint for a text string
Add-type -AssemblyName office
Add-type -AssemblyName microsoft.office.interop.powerpoint

$path = "C:test1"
$MatchString = "one"
$pptSlides = Get-Childitem -Path $path -Include *.ppt,*.pptx -Recurse

# Create instance of the PowerPoint.Application COM object
$ppt = New-Object -ComObject PowerPoint.Application
#$ppt.Activate()

# possibly many PowerPoint files in the folder so to stop PowerPoint app from appearing and disappearing
#$ppt.visible = $false
#$ppt.visible="msoTrue"
$ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue

# walk through the collection of slides
foreach($pptSlide in $pptSlides)
{

 write-host $pptSlide
 $presentation = $ppt.Presentations.open($pptSlide)

 # Max slides is determined for slide one
 $slcount = $ppt.ActivePresentation.Slides.Count 
 Write-Host "Total Slides = $slcount"

 ######################################
 #  Seems to be good to here
 ######################################

 # Max shapes is determined on just slide one.. as a test… this does not work
 $shcount = $ppt.ActivePresentation.Slides(1).Shapes.Count
 Write-Host "Total Shapes on Slide 1 = $shcount"

 # Walk through the collection of shapes

# not certain what to do here.   could maybe use $shcount if I could populate that
 ForEach($pptShape in $ppt.ActivePresentation.Slides($slcount).Shapes) {
     # trying to determine the shape type – want those of type text

     Write-Host $pptShape.Type
     # if type text then search for text goes here

     # if type is a spreadsheet then search for text – nice to have – may skip this
 }

 # prevent the Save Presentation prompt from displaying
 $presentation.saved = $true
 # close presentation
 $presentation.close()
 
 } #end for
 
 
 # release memory
 $ppt.quit()
 # release the memory immediately
 $ppt = $null
 # call garbage collection
 [gc]::collect()
 [gc]::WaitForPendingFinalizers()