Skip to content

Instantly share code, notes, and snippets.

@jongalloway
Created December 20, 2012 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongalloway/4343892 to your computer and use it in GitHub Desktop.
Save jongalloway/4343892 to your computer and use it in GitHub Desktop.
Add-type -AssemblyName office
$find = "SPEAKER"
$replace = "Jon Galloway"
function ReplaceText {
param(
[object]$shape,
[string]$find,
[string]$replace
)
if ($shape.HasTextFrame)
{
$textFrame = $shape.TextFrame
$textRange = $textFrame.TextRange
$textRange.Replace($find, $replace, 0, $msoFalse, $msoFalse)
<#
#Use this if above replacement causes formatting to be lost.
$paragraphs = $textRange.Paragraphs()
foreach ($paragraph in $paragraphs)
{
$text = $paragraph.Text
if($text.Contains($find)) {
$text = $text.Replace($find, $replace)
$paragraph.Text = $text
}
}
#>
}
}
$Application = New-Object -ComObject powerpoint.application
$msoTrue = [Microsoft.Office.Core.MsoTriState]::msoTrue
$msoFalse = [Microsoft.Office.Core.MsoTriState]::msoFalse
$application.visible = $msoTrue
$path = "C:\Users\Jon\SkyDrive\Projects\Git\"
Get-ChildItem -Path $path -Include "*.pptx" -Recurse |
ForEach-Object {
"Modifying $_.FullName"
#Open presentation with ReadOnly:False, Untitled:False, Visible:True
$presentation = $application.Presentations.Open($_.fullname, $msoFalse, $msoFalse, $msoTrue)
foreach ($slide in $presentation.Slides) {
foreach ($shape in $slide.Shapes) {
# [Microsoft.Office.Core.MsoShapeType]::msoGroup
if ($shape.Type -eq 6) {
foreach ($item in $shape.GroupItems) {
ReplaceText $item $find $replace
}
} else {
ReplaceText $shape $find $replace
}
}
}
$presentation.Save()
$presentation.Close()
}
$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
@SyntaxC4
Copy link

Why not pass a hash table into ReplaceText instead of a single find and replace value, then iterate over the collection?

@jongalloway
Copy link
Author

@SyntaxC4 Yep. I'd like to pass the hash in to the entire script and then into ReplaceText. How would you do that?

@birbilis
Copy link

as an [object] I guess, but doesn't it support Dictionary instead of HashTable?

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