none
Get active slide in Slide Master View, Powerpoint 2007/2010 RRS feed

  • Question

  • Hi

    I'm inserting images onto an active slide. This works well when in the Normal view. When I switch to the Slide Master View I get the following error: "sliderange cannot be constructed from a master"

    The following code causes the error: var index = _application.ActiveWindow.Selection.SlideRange.SlideIndex;

    What I'm trying to do is to get the active slide. I can't find any solution to this problem. the Master object doesn't have any support for getting an active slide.

    Thanks for help in advance,

    Martin

    Thursday, February 23, 2012 1:48 PM

All replies

  • Hi Martin,

    Thanks for your post.

    I think you need to ensure that the current view type is "ppViewNormal" before everytime inserting images. Using Application.ActiveWindow.ViewType would resolve the problem, see:

    http://msdn.microsoft.com/en-us/library/ff745686.aspx

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us


    Friday, February 24, 2012 6:14 AM
    Moderator
  • Hi Calvin_Gao,

    I think you misunderstand my problem. I want to get the active slide, no matter what view I'm in. You propose that I only get the active slide when I'm in the Normal view and skip the rest.

    I tried using this line in order to get the slide in any view: _application.ActiveWindow.View.Slide, but this returns different types (Com object when in Master slide view, and a Slide object when in Normal view). I don't know how to use this in different views.

    Friday, February 24, 2012 8:57 AM
  • Hi,

    Actually I mean you should always set this property to ppViewNormal before you do the customization, your procedure should look like below:

    Sub AddPicture()
    
        Dim presen As Presentation
        Dim sld As Slide
        Set presen = ActivePresentation
        Set sld = presen.Slides(1)
        'change the view type here
        Application.ActiveWindow.ViewType = ppViewNormal
        
        AddImageToSlide sld
    End Sub
    
    Sub AddImageToSlide(sld As Slide)
        'the code to insert images
    End Sub
    

    For more related information, you can refer to my reply in this thread:

    http://social.msdn.microsoft.com/Forums/en/officegeneral/thread/2dc6321f-c109-4c8d-84e1-c9dd4c19aa42

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us

    Friday, February 24, 2012 9:11 AM
    Moderator
  • thanks for fast reply:) I now have this code:

    _application.ActiveWindow.ViewType = PpViewType.ppViewNormal;
    int index = _application.ActiveWindow.Selection.SlideRange.SlideIndex;

    I still get the same error when I'm in the Slide Master View; "sliderange cannot be constructed from a master". I'm not interested in the first slide, but in the slide that the user has selected. I'm testing in Powerpoint 2010

    Friday, February 24, 2012 9:33 AM
  • An Update: It seems like a known bug is fooling us here. Take a look at this OLD KB from Microsoft: http://support.microsoft.com/kb/285436 

    yesterday I tried to print out the current ViewType, and I got ppViewNormal in both the Normal and in the Slide Master View. So it seems like the old KB hasn't been fixed in later versions?

    Because when I do this in the Slide Master View:

    Debug.WriteLine("My type before: " +_application.ActiveWindow.ViewType);
    _application.ActiveWindow.ViewType = PpViewType.ppViewNormal;
    Debug.WriteLine("My type after: " + _application.ActiveWindow.ViewType);
    Both printouts return ppViewNormal, and I get the error which I posted in my first post


    • Edited by MartinMyth Friday, February 24, 2012 9:47 AM
    Friday, February 24, 2012 9:46 AM
  • But again, Calvin_Gao, by setting the view to ppViewNormal doesn't change the Slide Master. Let's say I have one slide in my PPT. If I go to my Slide Master View where I select the third Office template and switch back to ppViewNormal then the slide I get as active is the first slide. When moving back to ppViewSlideMaster I have a Slide object which is NOT the active Slide Master.

    Am I missing something out?

    Edit: Remember that I WANT to add a picture to the Template, my Slide Master. I can't then switch to Normal View since my image won't be part of the Master.
    • Edited by MartinMyth Friday, February 24, 2012 12:28 PM
    Friday, February 24, 2012 12:26 PM
  • In VBA, you'd do like so to add a rectangle:

    ActiveWindow.View.Slide.Shapes.AddShape msoShapeRectangle, 0, 0, 100, 100

    While masters are not slides, and while intellisense gets rendered insensate by this, it works.


    Steve Rindsberg PowerPoint MVP since before dirt

    Sunday, February 26, 2012 4:49 AM
  • If adding objects, like images, in 2007 and 2010 was working like in 2010 with placeholders then I could just add the objects I wanted. But when the user has added placeholders and want to add images to the placeholders then it doesn't work in 2007 because the placeholders moves to where the image is being inserted, messing up the user's template.
    Monday, February 27, 2012 7:58 AM
  • A little bit late to the party, but found a solution to this problem in case you (or someone else) are still struggling: 

    By using a dynamic object, you can call the same Shapes.AddShape method on the View.Slide object, wether it's a Slide type or a COMObject; 

    dynamic s = Globals.ThisAddIn.Application.ActiveWindow.View.Slide;

    s.Shapes.AddShape(0,0,100,100);

    Works on slides, slidemasters and customlayouts.

    (Only tested in PowerPoint 2013)




    • Edited by jorgenho Friday, June 6, 2014 10:54 AM
    Friday, June 6, 2014 10:53 AM