VBA for Powerpoint

Hi,

I have been asked to modify a powerpoint presentation so that from a series of jpg files, one appears on the screen and then after a specific time period disappears and is replaced by another; this being a continuous loop on one slide only - like a photo tour. I have the following code, but need to get it up and running. Any suggestions:

Sub ImportABunch()

Dim strTemp As String
Dim strFileSpec As String
Dim oSld As Slide
Dim oPic As Shape

strFileSpec = C:\PFS Pictures\beach party *.PNG
'Ex. on a PC: C:\My Pictures\Flowers\*.PNG

strTemp = Dir(strFileSpec)

Do While strTemp <>
Set oSld = ActivePresentation.Slides.Add ppLayoutBlank
Set oPic = ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:=strFileSpec, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, _
Top:=0, _
Width:=100, _
Height:=100)

'Reset it to its real size
With oPic
.ScaleHeight 1, msoTrue
.ScaleWidth 1, msoTrue
End With

'Get the next file that meets the spec and go round again
strTemp = Dir
Loop

End Sub

It's giving me compile errors in the places in red. Any help would be greatly appreciated.

Cheers,

Michelle



Answer this question

VBA for Powerpoint

  • CoreStar2000

    This forum relates specifically to VB,Net code and not earlier version of VB, VBA, VB Script

    But I'll provide some useful information on places that may help

    If you want to actual write VB code within Powerpoint - powerpoint does have a version of VB although its not based upon .NET technology and is probably closer to a limited version of VB6. It is called VBA (visual basic for Applications) as you correctly have stated

    So often the best place is to get specific information about using writing VBA code for powerpoint

    Office Automation: office.developer.automation newsgroup

    http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.office.developer.automation&lang=en&cr=US

    http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=74&SiteID=1

    If you specifically wants to build a VB.NET based solution in (Outlook, Sharepoint, Infopath, Word, or Excel), then have a look in the Visual Studio Tools Office forums: http://forums.microsoft.com/MSDN/default.aspx forumgroupid=4&siteid=1

    This relates specifically to the wrtiting applications for the Office products of which powerpoint is one.

    I hope that provides a few places to check.

    As a point on the line

    Do While strTemp <>

    The line is incomplete.

    And you need to detail what the errors are saying. That way people will be able to help. Saying its giving me compile errors is not always the most precise description.



  • Espen Eriksmoen LOke

    Thanks heaps for that John!

    I've altered the code as below:

    Sub ImportABunch()

    Dim strTemp As String
    Dim strFileSpec As String
    Dim oSld As Slide
    Dim oPic As Shape

    strFileSpec = "C:\Documents and Settings\My Documents\My Pictures *.JPG"
    'Ex. on a PC: C:\My Pictures\Flowers\*.PNG

    strTemp = Dir(strFileSpec)

    Do While strTemp <> ""
    Set oSld = ActivePresentation.Slides.Add(ppLayoutBlank)
    Set oPic = ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:=strFileSpec, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, _
    Left:=0, _
    Top:=0, _
    Width:=100, _
    Height:=100)

    'Reset it to its real size
    With oPic
    .ScaleHeight 1, msoTrue
    .ScaleWidth 1, msoTrue
    End With

    'Get the next file that meets the spec and go round again
    strTemp = Dir
    Loop

    End Sub

    However, its giving me a compile error that states "Argument not optional" when it encounters the bit in red. Also, do i need to reference this code to a particular option on my slide show, or will it create a picture frame and insert the picture as the given size and at the given position in the code.

    Cheers,

    Michelle


  • Veduhin Evgeny

    Thanks for that. No run time errors occurring. Now just have to hook the code up. Will let you know how it goes.

    Cheers,

    Michelle


  • Marko Mihovilic

    Private Sub Image1_Click()

    Dim strTemp As String
    Dim strFileSpec As String
    Dim oSld As Slide
    Dim oPic As Shape

    strFileSpec = "C:\Documents and Settings\Michelle\My Documents\My Pictures\*.JPG"
    'Ex. on a PC: C:\My Pictures\Flowers\*.PNG

    strTemp = Dir(strFileSpec)

    Do While strTemp <> ""
    Debug.Print strTemp
    Set oSld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count, ppLayoutBlank)
    Set oPic = ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:=strFileSpec, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, _
    Left:=0, _
    Top:=0, _
    Width:=100, _
    Height:=100)

    'Reset it to its real size
    With oPic
    .ScaleHeight 1, msoTrue
    .ScaleWidth 1, msoTrue
    End With

    'Get the next file that meets the spec and go round again
    strTemp = Dir
    Loop

    End Sub


    Hi Jon,

    Tried the code with the backslash and am still getting a runtime error.

    Any further suggestions

    Cheers,
    Michelle


  • megs_lefevre

    Change this line

    strFileSpec = "C:\Documents and Settings\Michelle\My Documents\My Pictures *.JPG"

    to this

    strFileSpec = "C:\Documents and Settings\Michelle\My Documents\My Pictures\*.JPG"

    I have thought for a while that the missing backslash before *.JPG was part of the problem.

    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com/
    _______




  • GaryV

    spotty wrote:
    This forum relates specifically to VB,Net code and not earlier version of VB, VBA, VB Script

    Then why is the forum called "Visual Basic for Applications" There are plenty of other forums here that deal with the whole dot net universe.

    To the original post:

    this should work better:

    Do While strTemp <> ""
    Set oSld = ActivePresentation.Slides.Add (ppLayoutBlank)


    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com/
    _______




  • Rajat Solanky

    Thanx for that. I'll have another look at it a bit later on. Gotta get this java varsity assignment out of the way first!

    Cheers,
    Michelle


  • Dennis Mayores

    Two things.

    1. After this line:

    Do While strTemp <> ""

    insert this:

    Debug.Print strTemp

    which will list all the files that were inserted by the procedure.

    2. Is this the correct path

    "C:\Documents and Settings\Michelle\My Documents\My Pictures *.JPG"

    on my machine it would be

    "C:\Documents and Settings\Michelle\My Documents\My Pictures\*.JPG"

    I think you've left out the backslash.


    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com/
    _______



  • Anwar Hossain(shovon)

    Oh, Duh! Of course it can't find a file named *.JPG!

    Do it as follows to make it easier to hijack for other projects in the future.

    Private Sub Image1_Click()

    Dim strTemp As String
    Dim strPath As String
    Dim strExt as String

    Dim strFileSpec As String
    Dim oSld As Slide
    Dim oPic As Shape

    strPath = "C:\Documents and Settings\Michelle\My Documents\My Pictures"
    strExt = ".JPG"

    strFileSpec = strPath & "\*" & strExt
    'Ex. on a PC: C:\My Pictures\Flowers\*.PNG

    strTemp = Dir(strFileSpec)

    Do While strTemp <> ""
    Debug.Print strTemp
    Set oSld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count, ppLayoutBlank)
    Set oPic = ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
    FileName:=strPath & "\" & strTemp, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, _
    Left:=0, _
    Top:=0, _
    Width:=100, _
    Height:=100)

    'Reset it to its real size
    With oPic
    .ScaleHeight 1, msoTrue
    .ScaleWidth 1, msoTrue
    End With

    'Get the next file that meets the spec and go round again
    strTemp = Dir
    Loop

    End Sub



  • hinchi

    The way you set strFileSpec, the directory is

    C:\Documents and Settings\Michelle\My Documents

    and the file name is

    My Pictures *.JPG

    On my computer, there is a default path

    C:\Documents and Settings\Jon\My Documents\My Pictures

    which has some sample images in it which were installed with Windows. I still think this is part of your remaining difficulty.

    Did you insert the Debug.Print line What file names appeared in the Immediate Window

    It's not VBA accepting a JPG format anyway, it's PowerPoint accepting it, under orders of VBA.



  • Yseesee

    Okay,

    I've made the corrections you recommended, and its giving me a runtime error - specified file not found. I've double checked the file path and its definately correct, but the pics in the folder open up as JPEG's. This still can't be the problem as I've converted one of the picture files in the folder to a GIF and altered the code accordingly. Nothing happens. I've also tried changing the code to find JPEG's. When i run it and click on the picture frame in my opening slide, nothing happens. Does VBA accept JPEG as a format

    Cheers,
    Michelle


  • DW-DW

    Private Sub Image1_Click()

    Dim strTemp As String
    Dim strFileSpec As String
    Dim oSld As Slide
    Dim oPic As Shape

    strFileSpec = "C:\Documents and Settings\Michelle\My Documents\My Pictures *.JPG"

    strTemp = Dir(strFileSpec)

    Do While strTemp <> ""
    Set oSld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count, ppLayoutBlank)
    Set oPic = ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:=strFileSpec, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, _
    Left:=0, _
    Top:=0, _
    Width:=100, _
    Height:=100)

    'Reset it to its real size
    With oPic
    .ScaleHeight 1, msoTrue
    .ScaleWidth 1, msoTrue
    End With

    'Get the next file that meets the spec and go round again
    strTemp = Dir
    Loop

    End Sub

    The code compiles, i've checked that there are definately pictures in JPG format in My Pictures and i've hooked the code up to a picture object, but when i run the slide show nothing happens

    Thanx heaps for all your help! Much appreciated.

    Cheers
    Michelle


  • ktarayao

    The quickest way to get help in the VB Editor is using the Object Browser. If it's not visible, press the F2 key, or find it in the View menu. Find Slides in the left list, and click on Add in the right. In the bottom of the OB it says:

    Function Add(Index As Long, Layout As PpSlideLayout) As Slide

    So you're missing the slide index. Try this:

    Set oSld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count, ppLayoutBlank)

    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com/
    _______



  • Sune Henriksen

    Hi Jon,

    I've had another look at it. Under "My Pictures" if have a several .jpg files, one of which is 1089.jpg. When i'm running the program it displays only 1089.jpg in the immediate window and then a runtime error that the specified file wasnt found

    Cheers,

    Michelle


  • VBA for Powerpoint