OfficeTips Home || VBA Section || General Section || Download Section || Privacy Policy

Generic function to obtain active slide in any view
 

Show below is a generic function which returns the active slide object in any given view. This routines is really useful to determine various slide properties of the active slide. Two examples on how to use the function are provided too. The function requires the window object of the active view as an argument. Depending upon the window object it determines the view type and returns the slide object reference.


 

Function GetActiveSlide(ByVal oWnd As Object) As Object
Dim SlideNum As Integer
Dim oPres As Object 'Presentation

On Error GoTo ErrGetActiveSlide
Set oPres = oWnd.Presentation
SlideNum = 0
If oPres.Slides.Count > 0 Then
    Select Case TypeName(oWnd)
    Case "DocumentWindow"
        Select Case oWnd.View.Type
        Case ppViewSlideMaster
            SlideNum = -1
        Case ppViewTitleMaster
            SlideNum = -2
        Case ppViewSlideMaster
            SlideNum = -1
        Case ppViewTitleMaster
            SlideNum = -2
        Case ppViewNotesMaster
            SlideNum = -3
        Case ppViewHandoutMaster
            SlideNum = -4
        Case ppViewSlide, ppViewNotesPage
            SlideNum = oWnd.View.Slide.SlideIndex
        Case ppViewSlideSorter
            If oWnd.Selection.Type = ppSelectionSlides Then
                If oWnd.Selection.SlideRange.Count = 1 Then
                    SlideNum = oWnd.Selection.SlideRange.SlideIndex
                End If
            End If
        Case ppViewOutline
            If oWnd.Selection.SlideRange.Count = 1 Then
                SlideNum = oWnd.Selection.SlideRange.SlideIndex
            End If
        Case ppViewNormal
            Select Case GetViewType(oWnd)
            Case ppViewSlideMaster
                SlideNum = -1
            Case ppViewHandoutMaster
                SlideNum = -2
            Case ppViewNotesMaster
                SlideNum = -3
            Case ppViewTitleMaster
                SlideNum = -4
            Case 11 'ppViewThumbnails
                SlideNum = -5
            Case Else
                SlideNum = oWnd.View.Slide.SlideIndex
            End Select
        End Select
    Case "SlideShowWindow"
        SlideNum = oWnd.View.Slide.SlideIndex
    End Select
End If
Select Case SlideNum
Case Is > 0
    Set GetActiveSlide = oPres.Slides(SlideNum)
Case Is = -1
    Set GetActiveSlide = oPres.SlideMaster
Case Is = -2
    Set GetActiveSlide = oPres.TitleMaster
Case Is = -3
    Set GetActiveSlide = oPres.NotesMaster
Case Is = -4
    Set GetActiveSlide = oPres.HandoutMaster
Case Is = -5
    Set GetActiveSlide = oWnd.Selection.SlideRange(1)
Case Is = 0
    Set GetActiveSlide = Nothing
End Select
Exit Function
ErrGetActiveSlide:
Set GetActiveSlide = Nothing
End Function

Function GetViewType(oWnd As Object) As Integer
With oWnd
    If .ViewType = ppViewNormal Then
        GetViewType = .ActivePane.ViewType
    Else
        GetViewType = .ViewType
    End If
End With
End Function

Sub TestNormalWindow()
Dim TheSelectedSlide As Object
Set TheSelectedSlide = GetActiveSlide(ActiveWindow)
If TheSelectedSlide Is Nothing Then
MsgBox "Nothing appropriate is selected.", vbExclamation
Else
MsgBox "You selected a " & TypeName(TheSelectedSlide), vbExclamation
End If
End Sub

Sub TestShowWindow()
Dim TheSelectedSlide As Object
If SlideShowWindows.Count > 0 Then
    Set TheSelectedSlide = GetActiveSlide(SlideShowWindows(1))
End If
End Sub



This snippet restores the imported image shape to it's original dimensions and restores the defaults of other related properties like Contrast and Brightness. The routines assumes that the current selection is a picture on the slide.


Sub ResetPictureProperties()
With ActiveWindow.Selection.ShapeRange
    .LockAspectRatio = msoFalse
    .ScaleWidth 1, True, msoScaleFromTopLeft
    .ScaleHeight 1, True, msoScaleFromTopLeft
    .LockAspectRatio = True
    .PictureFormat.ColorType = msoPictureAutomatic
    .PictureFormat.Brightness = 0.5
    .PictureFormat.Contrast = 0.5
End With
End Sub

 



        

 

 

 

Copyright 1999-2014 (c) Shyam Pillai. All rights reserved.