How to programmatically change slide layout in PowerPoint?

I need to change the slide layout programmatically using C # (using Add-In Express 2009 for Office and .NET). If the new layout is predefined, then everything will be fine, but not, if I need to set my own layout as new (without re-creating the slides). Unfortunately, I did not find any information on how to do this, the PowerPoint object model reference documentation did not answer me either. There is only the option to create a new slide that uses a custom layout.

I conducted an experiment and made sure that the object Slideremained the same while I changed the layout of both predefined and custom ones. I do not want to create a new slide when I just need to switch the layout.

Is it possible at all? Please help me find a way to do this.

+5
source share
2 answers

The only way this will work is that your own layout is actually used in the deck. Then you just take this layout and apply it to the slide you want. You could programmatically create a new slide with your custom layout, use its layout to apply it to another slide, and then delete the new slide that you created. Here's the code for applying a custom layout (note that mine is a custom layout) ap.Slides(2)

Sub ChangeLayout()
    Dim ap As Presentation
    Set ap = ActivePresentation
    Dim slide1 As Slide
    Set slide1 = ap.Slides(1)
    Dim customLayout As PpSlideLayout
    customLayout = ap.Slides(2).Layout
    slide1.Layout = ly
End Sub
+4
source

, . , . , ....

private PowerPoint.CustomLayout DpGetCustomLayout(
        PowerPoint.Presentation ppPresentation, string myLayout)
{
   //
   // Given a custom layout name, find the layout in the master slide and return it
   // Return null if not found
   //
   PowerPoint.CustomLayout ppCustomLayout = null;

   for (int i = 0; i < ppPresentation.SlideMaster.CustomLayouts.Count; i++)
   {
       if (ppPresentation.SlideMaster.CustomLayouts[i + 1].Name == myLayout)
           ppCustomLayout = ppPresentation.SlideMaster.CustomLayouts[i + 1];
   }
      return ppCustomLayout;
}

, . , , . , . , .

. .

+2

All Articles