1
votes

I am trying to use PowerShell to pro-grammatically update notes in PowerPoint slide notes. Being able to do this will save tremendous amounts of time. The code below allows me to edit the notes field with PowerShell but it messes up the format each time.

$PowerpointFile = "C:\Users\username\Documents\test.pptx"
$Powerpoint = New-Object -ComObject powerpoint.application
$ppt = $Powerpoint.presentations.open($PowerpointFile, 2, $True, $False)
foreach($slide in $ppt.slides){
    if($slide.NotesPage.Shapes[2].TextFrame.TextRange.Text -match "string"){
        $slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = $slide.NotesPage.Shapes[2].TextFrame.TextRange.Text -replace "string","stringreplaced"
    }
}
Sleep -Seconds 3
$ppt.Save()
$Powerpoint.Quit()

For example, right now it will iterate through each slide's notes and update the word string to stringreplaced but then the entire notes text becomes bold. In my notes I have a single word at the top of the notes that is bold and then text below it. For example, a note on a slide my look like this:

Note Title

Help me with this string.

After PowerShell updates the notes field it saves it to a new .pptx file but the note now looks like this:

Note Title

Help me with this stringreplaced.

Any ideas on how to update slide notes without messing up any formatting found in the notes? It only messes up formatting for slides the script updates.

1

1 Answers

2
votes

When you change the entire text content of a textrange in PPT, as your code's doing, the changed textrange will pick up the formatting of the first character in the range. I'm not sure how you'd do this in PowerShell, but here's an example in PPT VBA that demonstrates the same problem and shows how to use PPT's own Replace method instead to solve the problem:

Sub ExampleTextReplace()
' Assumes two shapes with text on Slide 1 of the current presentation
' Each has the text "This is some sample text"
' The first character of each is bolded

' Demonstrates the difference between different methods of replacing text
'   within a string

    Dim oSh As Shape

    ' First shape: change the text
    Set oSh = ActivePresentation.Slides(1).Shapes(1)
    With oSh.TextFrame.TextRange
        .Text = Replace(.Text, "sample text", "example text")
    End With
    ' Result:  the entire text string is bolded


    ' Second shape: Use PowerPoint's Replace method instead
    Set oSh = ActivePresentation.Slides(1).Shapes(2)
    With oSh.TextFrame.TextRange
        .Replace "sample text", "example text"
    End With
    ' Result:  only the first character of the text is bolded
    '          as it was originally

End Sub