PPT97: Add-in that Inserts a Picture to Fit to the Slide

ID: Q215601


The information in this article applies to:
  • Microsoft PowerPoint 97 For Windows


SUMMARY

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.


MORE INFORMATION

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/overview/overview.asp

Steps to Create the Add-in

  1. Open a new presentation in PowerPoint.


  2. On the Tools menu, point to Macro, and click Visual Basic Editor.


  3. In the VBA Editor window click Module on the Insert menu.


  4. Type the following VBA code in the module window.


  5. 
    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 
  6. On the Debug menu, click Compile VBAProject.


  7. On the File menu click Close and Return to Microsoft PowerPoint.


  8. In PowerPoint click Save As from the File menu, and save the presentation as InsertPicture.ppt.


  9. On the File menu click Save As, and save the presentation as a PowerPoint add-in (.ppa).


  10. On the File menu, click Close.


  11. On the Tools menu click Add-Ins.


  12. Click Add New.


  13. Click to select InsertPicture.ppa, then click OK.


For additional information about writing a PowerPoint add-in, please see the following article(s) in the Microsoft Knowledge Base:
PPT: How to Create a PowerPoint 97 Add-In

Additional query words: OFF2000 OFF97 PPT add-in

Keywords : kbdta
Version : WINDOWS:97
Platform : WINDOWS
Issue type :


Last Reviewed: November 13, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.