Andy
Andy

Reputation: 217

Editing shapes in powerpoint using powershell

I have a powershell script that I used to create a powerpoint. I don't want to edit how it pastes since it was done using functions that I use for several different cases and in only one do I need to further edit things. However I am struggling to determine how to send a picture to the back and to resize the text in a table using powershell. I know the shape number of both items in question, however I am not sure how to edit them in a powerpoint. I have tried:

$ppo = New-Object -ComObject powerpoint.application 

$presentation = $ppo.Presentations.open($ppt)


$presentation.Slides[3].Shapes[6].sendtoback


$presentation.Slides[6].Shapes[5].Left = 50

$presentation.Slides[6].Shapes[5].Top = 50

$presentation.Slides[6].Shapes[5].Width = 600


$presentation.Slides[6].Shapes[5].Height = 350

$presentation.Slides[6].Shapes[5].TextFrame.TextRange.Font.size = 9

$presentation.saveas($ppt)

$presentation.close()

To no avail. I successfully edited the table size, but it says there is no size property to font. Also, send to back doesn't appear to do anything. Is there an easy way to do this that I am missing?

Upvotes: 0

Views: 1639

Answers (2)

Andy
Andy

Reputation: 217

I ended up using:

$ppo = New-Object -ComObject powerpoint.application 

$ppo.Visible ='msotrue'

$presentation = $ppo.Presentations.open($FinalPPT)


$presentation.Slides[3].Shapes[6].Select()

$ppo.CommandBars.ExecuteMso("ObjectSendToBack")

and

$presentation.Slides[6].Shapes[5].Select()

$ppo.CommandBars.ExecuteMso("FontSizeIncrease")

$presentation.saveas($FinalPPT)

$presentation.close()

$ppo.quit()

Stop-Process -name POWERPNT -Force

Upvotes: 0

postanote
postanote

Reputation: 16096

Putting this here as it is too big for a normal comment.

Why are you not looking at the properties for each of the objects you are after, to determine if the property is there before trying to use it as well as discovering the numerical assignment of the object you are trying to work with.

For example:

$ppo = New-Object -ComObject powerpoint.application 
$ppo.Visible = [Microsoft.Office.Core.MsoTriState]::msoTrue

$presentation = $ppo.Presentations.open($ppt)

$presentation.Slides[3].Shapes | 
Select -First 1

$ppo | 
Get-Member | 
Out-GridView

$ppt | 
Get-Member | 
Out-GridView

$presentation | 
Get-Member | 
Select Method |
Out-GridView

$presentation.Slides[3].Shapes[1] | 
Get-Member | 
Out-GridView

Just use the filter in OGV to find all methods available, etc. Once you see the list navigate the OM as defined.

# Create a new deck
Add-type -AssemblyName office
$Application = New-Object -ComObject powerpoint.application
$application.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue

$slideType = 'microsoft.office.interop.powerpoint.ppSlideLayout' -as [type]

$blanklayout = $slideType::ppLayoutTitleOnly

$presentation = $application.Presentations.add()

$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 15)

# Maually add a few objects, then manully run these lines to see the changes
# Change the font of the main window
$presentation.Slides[1].Shapes[1].TextFrame.TextRange.Font.size = 42

# Select a shape
$presentation.Slides[1].Shapes[3].Select()

# Change the ZOrder
$Application.ActiveWindow.Selection.ShapeRange.ZOrder(3)

# Clean-up
$Application.quit()
$Application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
Stop-Process -Name POWERPNT -ErrorAction SilentlyContinue -Force

Upvotes: 1

Related Questions