none
Identify Selected MasterSlides from powerpoint presentation RRS feed

  • Question

  • Hello.. I have created an application with C# .net and powerpoint2007 (vsto addin),  i am using code "presntation.SlideMaster.Background.Fill.UserPicture(PictureFileName);" to set background of SlideMaster, but its apply only the first SlideMaster in a presentation. when i create 2 or more SlideMaster in a single presentation , i need to select the current SlideMaster and apply the picture as background... How can i locate selected SlideMaster from a presenation and set background using c# code.. Pls help me soon


    Tuesday, May 10, 2011 10:06 AM

All replies

  • Hi Noufy,

    AFAIK, Slide masters apply either to the entire presentation and is then referenced by the ActivePresentation object, "ppt.ActivePresentation.SlideMaster" or they can be created to apply to Slide or SlideRange objects and are then referenced such as "ppt.ActivePresentation.Slides(5).Master".

    So to get your PictureFileName into a custom "slide master" background you will have to assign that custom master to a slide or range of slides in the active presentation.

    Hope this helps


    Regards, Rich
    Tuesday, May 10, 2011 12:12 PM
  • Thanx.... I have checked it with "ppt.ActivePresentation.Slides.Range(5).Master",  but its return the Range is 1 and make error.. do you have any other solns ?
    Tuesday, May 10, 2011 12:47 PM
  • Can you send a code snippet and the exact error message you are getting
    Regards, Rich
    Tuesday, May 10, 2011 2:11 PM
  • Code:

    PowerPoint.Application powerpointAppln = new PowerPoint.Application();
    PowerPoint.Presentation presntation = powerpointAppln.ActivePresentation;

    presntation.Slides.Range(5).Master.Background.Fill.UserPicture(ImageFileName);

     

    Error:

    -----

    System.Runtime.InteropServices.COMException (0x80048240): Slides (unknown member) : Integer out of range. 5 is not in the valid range of 1 to 1.

       at PowerPoint.Slides.Range(Object index)

    -----------

    In the presentation , i have created more than 5 master slides.. but its take range as 1.... Also i need to set the background of a perticular slide under master slide ....

    Wednesday, May 11, 2011 4:05 AM
  • You can only apply a custom slide master to a slide or slide range that actually exists in your presentation. The error message is quite clear, you don't have a slide # 5 in your presentation.

    You only have 1 slide.


    Regards, Rich
    Wednesday, May 11, 2011 10:23 AM
  • Thanx ....How can i apply custom slide master to a slide or slide range? when i have set the powerpoint presentation as slide master, Range and count taken as 1 when i create a lot of slides ... i am using following code to get the slide count , and working with selected slides. 

    PowerPoint.Application powerpointAppln = new PowerPoint.Application();
    PowerPoint.Presentation presntation = powerpointAppln.ActivePresentation;
    object slideId = powerpointAppln.ActiveWindow.Selection.SlideRange.Count;
    powerpointAppln.ActiveWindow.Selection.SlideRange.Item(slideId).FollowMasterBackground = Office.MsoTriState.msoFalse;
    powerpointAppln.ActiveWindow.Selection.SlideRange.Item(slideId).Background.Fill.UserPicture(backFileName);

    when slide view in normal type its working good, but in the case of slide master it shows following error... how can i solve this..pls give me soln with sample codes

    error:

    -----------------

    System.Runtime.InteropServices.COMException (0x80048240): Selection (unknown member) : Invalid request.  SlideRange cannot be constructed from a Master.

       at PowerPoint.Selection.get_SlideRange()

     

     

    Wednesday, May 11, 2011 11:02 AM
  • To run a test I used PowerPoint slide file that had 12 slides in it. I don't write in C# so I used the following VB code.

        Dim ppt As Presentation
        Set ppt = ActivePresentation
       
        Dim slideId As Integer
        slideId = ppt.Slides.Count
       
        ppt.Slides(slideId).FollowMasterBackground = msoFalse
        ppt.Slides(slideId).Background.Fill.UserPicture "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg"

    It changes the background of slide 12 to the Winter.jpg image.

    Hope this helps


    Regards, Rich
    Wednesday, May 11, 2011 3:11 PM
  • Thanx for ur reply... its working good if the presentation is normal view, if we set the presentation as slide master , its not working .Actually, i want to apply background for selected slide in Slide master ... pls help me..

    Thursday, May 12, 2011 4:09 AM
  • The following will add a new custom layout master and then set the background.

        Dim ppt As Presentation
        Set ppt = ActivePresentation
       
        Dim ct As Integer
        ct = ppt.SlideMaster.CustomLayouts.Count
        ct = ct + 1
        ppt.SlideMaster.CustomLayouts.Add (ct)
        ppt.SlideMaster.CustomLayouts.Item(ct).FollowMasterBackground = msoFalse
        ppt.SlideMaster.CustomLayouts.Item(ct).Background.Fill.UserPicture "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg"


    Regards, Rich
    • Proposed as answer by Bruce Song Friday, June 3, 2011 12:47 PM
    • Unproposed as answer by Bruce Song Monday, June 6, 2011 2:51 AM
    Thursday, May 12, 2011 9:40 PM
  • Thanx ..., Its working pretty good.. How can i get selected slide number  from slidemaster? My code is working with normal view, but not with slidemaster.. Pls help me soon..
    Monday, May 16, 2011 2:33 PM
  • Adding, updating or finding a slide’s custom layout directly by name reference creates an exception.  Only referencing the custom layout by number actually functions. For example this works:

            ppt.Slides(1).CustomLayout = ppt.SlideMaster.CustomLayouts(1)

    This does not work:

            ppt.Slides(1).CustomLayout = ppt.SlideMaster.CustomLayouts(“MyName”)

    So to add a custom layout and then apply it to new or existing slides using the name of the custom layout as the referece you need to do something like this.

    Create a custom layout and assign it a name.

    Sub CreatNewCustomLayout()

        Dim ppt As Presentation

        Dim ct As Integer

        Set ppt = ActivePresentation

        ct = ppt.SlideMaster.CustomLayouts.Count

        ct = ct + 1

        ppt.SlideMaster.CustomLayouts.Add (ct)

        ppt.SlideMaster.CustomLayouts.Item(ct).Name = "MyName"

        ppt.SlideMaster.CustomLayouts.Item(ct).FollowMasterBackground = msoFalse

        ppt.SlideMaster.CustomLayouts.Item(ct).Background.Fill.UserPicture "C:\~\Winter.jpg"

    End Sub

    Add or update a slide’s custom layout by name.

    Sub CustomLayoutToSlide(clName As String, AddSw As Boolean, SlideNumber As Integer)

        'If AddSw = True a new slide with the custom layout name of clName's value is added

        'If AddSw = False the slide number indicated by the value of SlideNumber is

        'updated with the custom layout name of clName's value

        Dim ppt As Presentation

        Dim clNbr, sCount As Integer

        Dim slideLayout As CustomLayout

        Set ppt = ActivePresentation

        sCount = ppt.Slides.Count

        For Each slideLayout In ppt.SlideMaster.CustomLayouts

            If slideLayout.Name = clName Then

                clNbr = slideLayout.Index

            End If

        Next slideLayout

        If AddSw = True Then

            ppt.Slides.AddSlide sCount + 1, ppt.SlideMaster.CustomLayouts(clNbr)

        Else

            ppt.Slides(SlideNumber).CustomLayout = ppt.SlideMaster.CustomLayouts(clNbr)

        End If

    End Sub

    To find the custom layout that is applied to each slide.

    Sub WhatLayoutApplied()

        Dim ppt As Presentation

        Dim sld As Slide

        Set ppt = ActivePresentation

        For Each sld In ppt.Slides

            Debug.Print "Index = " & sld.CustomLayout.Index & " Name = "; sld.CustomLayout.Name

        Next sld

     

    End Sub

    Obviously if you only reference the layout via a known numeric index some of this code can be eliminated. In addition, to my knowledge you cannot directly find the slide number from the slide master property. However, it probably is available in the presentation's xml.

    Hope this helps

       
    Regards, Rich http://www.greatcirclelearning.com

    Tuesday, May 17, 2011 5:59 PM
  • Thanx for your reply... I think it cannot get manually Selected powerpoint slide from slidemaster....please give me solution for selected slide ...
    Wednesday, May 18, 2011 4:44 AM
  • This command will return the currently selected slide number

    ppt.Windows(1).View.Slide.SlideNumber


    Wag and they wag with you, Howl and they put you outside. http://greatcirclelearning.com
    Wednesday, May 18, 2011 6:46 PM
  • Thanx.... I think ,its working with VB.NET , but in c# it is not possible... do u have any way in c#.net ..pls help me.
    Monday, May 23, 2011 7:30 AM