OfficeTips Home || VBA Section || General Section || Download Section || 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.

  1. Name the toggle states (On/Off) of the images in the following manner: On - anynameA.ext,  Off - anynameB.ext.
    e.g ButtonA.gif; ButtonB.gifInsert a linked image (buutonA.gif) shape into the presentation.

  2. Set the action settings of the macro to run the macro given below.

  3. Run the show and test it by clicking on the shape.

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.
 


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-2018 (c) Shyam Pillai. All rights reserved.