Home | VBA Section | General Section | Downloads | Licensing | Privacy Policy

 How to do an image toggle in PowerPoint
 

The code given below is designed to toggle the image in the shape with another predefined one. This can be extended to any number of images if required. It is a generic routine and will work with any shape on any slide provided it is a linked image.

  • Name the toggle states (On/Off) of the images in the following manner
    On - anynameA.ext
    Off - anynameB.ext
    e.g ButtonA.gif; ButtonB.gif
  • Insert a linked image (buutonA.gif) shape into the presentation.
  • Set the action settings of the macro to run the macro given below.
  • Run the show and test it by clicking on the shape.
    
'
        --------------------------------------------------------------------------------
' Copyright ©1999-2007, Shyam Pillai, All Rights Reserved.
' --------------------------------------------------------------------------------
' You are free to use this code within your own applications, add-ins,
' documents etc but you are expressly forbidden from selling or
' otherwise distributing this source code without prior consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
' --------------------------------------------------------------------------------
Sub SwapPicture(oShp As Shape)
    Dim SourceLink As String
    Dim FileExtn As String
' TglVal stores either 'a' or 'b'
    Dim TglVal As String

    If oShp.Type = msoLinkedPicture Then
        With oShp.LinkFormat
' Store the original link; faster manipulating the string
            SourceLink = .SourceFullName
' Store the image file extension plus '.' to concatenate it later
            FileExtn = Right(SourceLink, 4)
            TglVal = IIf(LCase(Left(Right(SourceLink, 5), 1)) = "a", "b","a")
            ' Concatenate strings to point link to new image.
            .SourceFullName = Left(SourceLink, Len(SourceLink) - 5) & _
                                                  TglVal & FileExtn
        End With
       
    End If
End Sub

        

 


 


This code snippet illustrates the manner in which PowerPoint can be automated from any external application. In this example a slide in inserted into a new presentation and then an image is imported into the slide and scaled to it's original size.

You can run this code from any Office application or Visual Basic® but don't forget to add a reference in the project to the PowerPoint object library and the Office object library.
 


' --------------------------------------------------------------------------------
' Copyright ©1999-2007, Shyam Pillai, All Rights Reserved.
' --------------------------------------------------------------------------------
' You are free to use this code within your own applications, add-ins,
' documents etc but you are expressly forbidden from selling or
' otherwise distributing this source code without prior consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
' --------------------------------------------------------------------------------

Sub InsertGraphicOnSlide()

    Dim ppApp As PowerPoint.Application
    Dim ppPres As PowerPoint.Presentation
    Dim ppShape As PowerPoint.Shape
    Dim ppCurrentSlide As PowerPoint.Slide

    Set ppApp = CreateObject("PowerPoint.Application")
    ppApp.Visible = True

    Set ppPres = ppApp.Presentations.Add(msoTrue)
    Set ppCurrentSlide = ppPres.Slides.Add(Index:=1, _
                                           Layout:=ppLayoutBlank)

    With ppCurrentSlide.Shapes
        ' Adds a picture to slide 1 in the active presentation.
        Set oPicture = .AddPicture("D:\Area 51\Images\test.jpg", _
                                   msoFalse, msoTrue, 0, 0, 1, 1)
        ' Now scale the image
        oPicture.ScaleHeight 1, msoTrue
        oPicture.ScaleWidth 1, msoTrue

    End With
End Sub
 

        

 



        

 

 

 

Copyright © 1999-2008 Shyam Pillai. All rights reserved.