| PPT97: Add-in that Inserts a Picture to Fit to the SlideID: Q215601 
 | 
When you insert a picture into a PowerPoint 97 slide, the picture does not fill the full slide area. You can create a PowerPoint add-in to emulate the behavior seen in PowerPoint 4.0 and PowerPoint 95.
Microsoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and debug
procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to
provide added functionality or construct procedures to meet your specific
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/
Sub InsertPictureToFitSlide()
'*************************************************************
' Purpose:
' This macro mimics the way in which previous versions 
' of PowerPoint inserted a picture into a PPT slide, i.e., 
' the picture is resized to occupy most of the slide area. 
'
' This macro uses the method described in Knowledge Base 
' article Q168649, "PPT: Sample VBA Code to Insert an Image 
' Full Size and Centered" to resize the picture after it is 
' inserted.
'
'*************************************************************
    Dim oPicture As ShapeRange  ' Object for inserted picture
    Dim sPictureWidth As Single
    Dim sPictureHeight As Single
    Dim sSlideWidth As Single
    Dim sSlideHeight As Single
    Dim sScaleFactor As Single
    
    ' Show Insert Picture from File dialog
    ' Note: PowerPoint 97 does not provide a built-in
    ' dialog to do this.
    SendKeys "%IPF", True
    ' Set oPicture to the shape range, which will be the
    ' inserted picture
    
    On Error GoTo ErrorHandler
    Set oPicture = ActiveWindow.Selection.ShapeRange
    
    ' Get current picture width and height
    sPictureWidth = oPicture.Width
    sPictureHeight = oPicture.Height
    
    ' Get slide width and height
    sSlideWidth = ActivePresentation.PageSetup.SlideWidth
    sSlideHeight = ActivePresentation.PageSetup.SlideHeight
    
    ' Get scale factor that will fit picture to slide
    If sPictureWidth > sPictureHeight Then
        sScaleFactor = sSlideWidth / sPictureWidth
    Else
        sScaleFactor = sSlideHeight / sPictureHeight
    End If
    
    ' Make picture a little smaller than slide
    sScaleFactor = sScaleFactor - 0.15
    
    ' Resize picture
    oPicture.ScaleHeight CSng(sScaleFactor), msoTrue
    oPicture.ScaleWidth CSng(sScaleFactor), msoTrue
           
    ' Move the picture to the center of the slide, 
    ' and select it
    With ActivePresentation.PageSetup
        oPicture.Left = (.SlideWidth \ 2) - _
            (oPicture.Width \ 2)
        oPicture.Top = (.SlideHeight \ 2) - _
            (oPicture.Height \ 2)
        oPicture.Select
    End With
ErrorHandler:
End Sub
Sub Auto_Open()
'*************************************************************
' Macro Auto_Open
'
' Purpose:
' The Auto_Open macro stores initialization code for an add-in and is
' automatically executed when the add-in is loaded by PowerPoint. The
' following code
'
'   1. adds a command bar (Insert Picture) to the CommandBars
'      collection and then adds a button to the command bar. When the
'      user clicks the button, the code in InsertPictureToFitSlide
'      runs to perform the insert, resize, and group/ungroup.
'
'   2. adds a menu command (Insert picture to fit slide) to the Tools
'      menu. When the user presses Alt+I,N, the code in
'      InsertPictureToFitSlide runs as described above.
'
' Please refer to Knowledge Base article Q163461, "PPT: How to Create a
' PowerPoint 97 Add-In," for information on creating a PPT add-in.
'
'*************************************************************
    ' For adding custom command bar
    Dim oInsertPictureCommandBar As CommandBar
    Dim oInsertPictureControl As CommandBarButton
    
    ' For adding new entry to Tools menu
    Dim oNewToolsControl As CommandBarControl
    Dim oToolsMenu As CommandBars
    
    ' Create a new command bar and add a command button to it
    Set oInsertPictureCommandBar = _
        CommandBars.Add(Name:="Insert Picture", _
        Position:=msoBarRight)
    
    oInsertPictureCommandBar.Visible = True
        
    Set oInsertPictureControl = CommandBars("Insert Picture").Controls _
        .Add(Type:=msoControlButton)
    With oInsertPictureControl
        .FaceId = 1362
        .OnAction = "InsertPictureToFitSlide"
        .TooltipText = "Insert picture to fit slide"
        .Caption = "Insert Picture"
        .DescriptionText = "Inserts a picture and resizes " & _
                           "it to fill the slide"
        .Visible = True
    End With
    
    ' Add a new command to the Tools menu
    Set oToolsMenu = Application.CommandBars
    
    ' Create the menu choice. The choice is created in the first
    ' position in the Tools menu.
    Set oNewToolsControl = oToolsMenu("Tools").Controls.Add _
                    (Type:=msoControlButton, _
                     Before:=1)
    ' Name the command.
    oNewToolsControl.Caption = "I&nsert picture to fit slide"
    
    ' Connect the menu choice to your macro. The OnAction property
    ' should be set to the name of your macro.
    oNewToolsControl.OnAction = "InsertPictureToFitSlide"
End Sub
Sub Auto_Close()
'*************************************************************
' Macro Auto_Close
'
' Purpose:
' The Auto_Close macro is executed when an add-in is unloaded by
' PowerPoint. The Auto_Close macro stores clean-up code. The following
' code removes the command bar that was added in the Auto_Open Macro
' and the command that was added to the Tools menu.
'
'*************************************************************
    ' For deleting custom command bar
    Dim oBar As CommandBar
    
    ' For deleting command from Tools menu
    Dim oControl As CommandBarControl
    Dim oToolsMenu As CommandBars
    
    ' Delete the custom Insert Picture command bar
    For Each oBar In CommandBars
        If (oBar.BuiltIn = False) And _
           (oBar.Name = "Insert Picture") Then
            oBar.Delete
        End If
    Next    ' For Each oBar
    
    ' Delete the Insert Picture command from the Tools menu
    Set oToolsMenu = Application.CommandBars
    
    ' Loop through the commands on the Tools menu
    For Each oControl In oToolsMenu("Tools").Controls
    
        ' Check to see whether the command exists
        If oControl.Caption = _
             "I&nsert picture to fit slide" Then
        
            ' Check to see whether action setting is set to 
            ' ChangeView.
            If oControl.OnAction = _
                "InsertPictureToFitSlide" Then
                ' Remove the command from the menu.
                oControl.Delete
            End If
        End If
    Next oControl   ' For Each oControl
    
End Sub Additional query words: OFF2000 OFF97 PPT add-in
Keywords          : kbdta 
Version           : WINDOWS:97
Platform          : WINDOWS 
Issue type        : Last Reviewed: February 13, 1999