Created
December 20, 2012 08:49
-
-
Save jongalloway/4343892 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Yep. I'd like to pass the hash in to the entire script and then into ReplaceText. How would you do that?
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
Why not pass a hash table into ReplaceText instead of a single find and replace value, then iterate over the collection?