Control PowerPoint from PowerShell Introduction Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a subscriber asked how to uppercase all the titles in a powerpoint presentation using vba this video will be how to do that but with powershell instead of vba and it will be a general introduction to controlling powerpoint with powershell probably the subscriber had a presentation with many title layout slides so it wasn't practical to change all the title text manually something like this this was generated with powershell and we'll see that in the end we'll assume the presentation is already open for this video i am trying out powershell studio 2020 instead of the powershell ise or a text editor such as visual studio code in the end powershell code is all text so everything in this video will work equally well in ise or any text editor i have tested it in ise as well an integrated development environment such as powershell studio will definitely make some parts of the development easier though if you are following along in powershell studio go to file options editor assemblies the plus sign and select microsoft.office.interop.powerpoint this is strictly optional but it will make your development easier in powershell studio if you're following along in ise or visual studio code just skip this step we'll end up with the same code no matter what the editor is it's just that we'll be able to browse and point and click with powershell studio rather than type some of the text manually powerpoint libraries or assemblies allow us to control powerpoint from powershell but these powerpoint assemblies are not usable by default in powershell so we have to reference them add type assembly name microsoft.office.interop.powerpoint in vba this is the same thing as tools references another way to do this is with system.reflection.org load with partial name microsoft.office.interop.powerpoint but load with partial name has been deprecated in powershell in favor of add type if you have an older version of powershell and ad type is not working go ahead and use load with partial name instead it works too now let's get a new powerpoint application this is the application as a whole not any particular presentation ppt equals new object com object powerpoint.application we'll write a powershell function called convert title uppercase that will take the presentation in which to change the titles we'll send it the only open presentation the one that has the focus the active presentation convert dash title uppercase dash presentation dollar ppt dot active presentation let's create the function function convert dash title uppercase now we define the function parameters in many languages you define the parameters a function takes in the same line as the function definition within parentheses not powershell though powershell has a param block param our parameter name will be presentation dollar presentation this would be legal as it is you don't have to specify the parameter type but you should because it will enable autocomplete and better document your code cast to microsoft.office.interop.powerpoint.presentationclass now let's iterate over all the slides in the parameter presentation for i equals one i is less than or equal to presentation.slides.count i plus plus vba collections start at one not zero now we will find the shape on the slide that is the title shape dollar s equals find title dash slide presentation dot slides index i so we need to write a find title powershell function that will be parameterized on a powershell slide object and return the title shape function find title we define the parameters the same way as the last function param the parameter name will be slide dollar slide again we could get away with parameters just like this without defining the type we know at development time that the slide type name is microsoft.office.interrupt.powerpoint.slide however in this case i couldn't get powershell to cast the slide object to microsoft.office.interop.powerpoint.slide or underscore slide or slide class if you know how to strongly type this parameter let me know in the comments below so we won't specify a type for the slide parameter you should always try to specify a type for parameters and only fall back like this as the last resort the consequence of not specifying the type here will be less help from powershell at development time and less documented code basically we just have to know the properties and methods on the slide object from memory and documentation now we will iterate over all the shapes on the parameter slide for i equals one i is less than or equal to slide.shapes.count i plus plus vba collections are one based not zero based next we will get the shape name since powerpoint inserts these with the naming pattern that will help us find the title shape shape name equals slide dot shapes i dot name cast to string we cast the name to a string so we can get auto-complete the test of whether a shape is a title is if it starts with title at least that is one way to do it if shape name that starts with title if we find the title shape we'll return it right away we assume there is only one title on each slide return slide dot shapes index i so this function assumes there will be one title shape per slide the logic would have to be adjusted if that is not true now back in the convert title uppercase function we'll get the text of the title shape title text equals dollar s dot text frame dot text range dot text cast to string first we cast the text to a string to get autocomplete this assumes all titles will have text if some titles do not have text we need extra guards such as has text now finally this is where we uppercase the title s.textframe.txtrange.txt equals title text to upper now let's run this ctrl f5 in powershell studio or f5 in powershell ise see that the titles have all been uppercased the rest of the code is not necessary if you just want to uppercase but stick around if you want a more general introduction to powershell we will make our powershell functions advanced by adding a little decoration to the param blocks commandlet binding this is literally called an advanced function in powershell now which sounds a little intimidating the difference between a plain old vanilla function and an advanced function is not that much though one difference is that our function can now accept the common parameters the common parameter we'll be using is verbose when called with dash verbose the function can write extra debugging style information that may not always be appropriate in convert title uppercase we'll add verbose output what the title was changed from and to right verbose changed title placeholder 0 to placeholder 1 dash f title text title text dot to upper in find title we'll add verbose output when the title shape is found and when it isn't right verbose placeholder 0 is the title shape dash f shape name right verbose placeholder 0 is not a title shape dash f shape name now let's add comment based help it starts with less than bang and ends with bang greater and has keywords within it that are parsed and displayed nicely in help so now we can run help find title or help convert title uppercase and if we run again with the verbose parameter we'll see the verbose output convert title uppercase dash verbose dash presentation ppt.activepresentation now let's see how we can create the example starting presentation with powershell function new example presentation we'll use the application and scope of the whole script to make a new presentation now let's say we want 25 slides so we loop from 1 to 25 for i equals 1 i is less than or equal to 25 i plus plus i will be the slide index and vba collections are one based add a slide slide equals prez dot slides dot add i microsoft.office.interop.powerpoint.ppslidelayout pplayouttitle the slides collection add method takes the index of the slide where the slide goes and the slide layout instead of microsoft.office.interrupt.powerpoint pp slide layout pp layout title you can substitute 20. this is available in the documentation we'll use the find title function earlier to get the title shape title shape equals find title dash slide slide now make the text something that will be easy to verify titleshape.txtframe.txtrange.txt equals abcdefghoj return the presentation return pres running it we get the example presentation new example presentation
Info
Channel: cbttjm
Views: 492
Rating: 5 out of 5
Keywords: PowerPoint, PowerShell, Office, COM, Slides, Interop, Advanced Functions, comment-based help
Id: G75I2amfxEQ
Channel Id: undefined
Length: 11min 49sec (709 seconds)
Published: Wed Aug 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.