Answered Macro to "bring to front" a picture

  • Saturday, June 26, 2010 9:35 AM
     
     

    Hi,

    I'm completely new to macros and to VBA although I have done a tiny bit of work in VB6. What I would really want is a list of commands that I can use in VBA, but I  can't seem to find one.

    The general project is:

    I have a PowerPoint presentation in PowerPoint 2010 and I have 18 images that give a 360 view of a particular molecule. I want the user to be able to move the mouse over a bar made out of 18 little squares, and when the cursor is over each square, a corresponding image is shown. Basically, a system where the user can spin the 360 view of the image using mouse over.

    I was planning to have each of the squares open a macro when the cursor is over them, and the macro would bring the corresponding image to the front. However, I cannot record the macro because PowerPoint 2010 doesn't do that for some reason and I haven't got a clue how to write it manually. Is what I'm trying to do even possible, or could there be a much simpler way to do it?

    I'd really appreciate any help, or even a link to a list of commands. I looked in the "library" and "learn" and nothing had anything about VBA. I also searched for "Bring to Front" in the help file in VBA to no avail.

    Thanks in advance,

    David.

All Replies

  • Saturday, June 26, 2010 10:59 AM
     
     
  • Saturday, June 26, 2010 12:49 PM
     
      Has Code

    Those links are great and I now know the best way to achieve what I want would be to create a box then change its fill instead of bringing 1 out of 18 images to the front (since there is no 'bring to front' method).

    I can create the box but I can't workout how to fill it using a separate macro. I can create it and fill it at the same time:

    Sub pic()
    Set myDocument = ActivePresentation.Slides(1)
    With myDocument.Shapes
      .AddShape(msoShapeRectangle, 0, 0, 200, 100).Fill _
        .UserPicture "c:\windows\tiles.bmp"
    End With
    End Sub
    but can't separate the two processes: 
    Sub pic()
    Set myDocument = ActivePresentation.Slides(1)
    myDocument.Shapes.AddShape Type:=msoShapeRectangle, _
      Left:=300, Top:=200, Width:=200, Height:=100
    End Sub
    
    Sub pic1()
    Set myDocument = ActivePresentation.Slides(1)
    With myDocument.Shapes
      .Fill _
        .UserPicture "c:\windows\tiles.bmp"
    End With
    End Sub

    How do I fill a shape that has not just been added?

    Thanks again,

    David.

  • Saturday, June 26, 2010 7:49 PM
     
     Proposed Answer

    To simplify your code, you can define a name of the added shape:

     

    Sub pic()

       Set myDocument = ActivePresentation.Slides(1)

       With myDocument.Shapes

         .AddShape(msoShapeRectangle, 0, 0, 200, 100).Fill _

           .UserPicture "c:\windows\tiles.bmp"

       End With

       myDocument.Shapes(myDocument.Shapes.count).name="Tiles"  ' or whatever works for you

    End Sub

     

    Now you can refer to a specific shape by a name. For example, to change the picture of this shape you can add code:

     

    Dim sh as Shape

    Set sh = myDocument.Shapes("Tiles")
    sh.Fill.UserPicture "c:\windows\OtherPicture.bmp"

     

    Read http://msdn.microsoft.com/en-us/library/aa220958(office.11).aspx to learn more about fill formatting for a shape.

     

    Nadia

     

    • Proposed As Answer by NadiaSz Saturday, June 26, 2010 7:56 PM
    •  
  • Sunday, June 27, 2010 10:04 AM
     
      Has Code

    Hi,

    It still doesn't seem to work. It's creating the new shape with the correct fill and I think it's giving it the name "Tiles" (how can I check?). However when I try to run the part that should change the fill it gives an error message of "Object Required" indicating line 11.

    Sub pic()
    Set myDocument = ActivePresentation.Slides(1)
    With myDocument.Shapes _
        .AddShape(msoShapeRectangle, 0, 0, 200, 100)
      .Name = "Tiles"
      .Fill.UserPicture "C:\Users\alband\Documents\ProDT\C4\C41.jpg"
    End With
    End Sub
    Sub pic_change()
    Dim sh As Shape
    Set sh = myDocument.Shapes("Tiles")
    sh.Fill.UserPicture "C:\Users\alband\Documents\ProDT\C4\C42.jpg"
    End Sub

    Also, what does the "sh" mean?

    Thanks again for all this help; really apriciate it!

  • Sunday, June 27, 2010 12:50 PM
     
     Answered

    Hello,

    Your change procedute is missing the line, please adjust it:

    Sub pic_change()
       Set myDocument = ActivePresentation.Slides(1)
       Dim sh As Shape
       Set sh = myDocument.Shapes("Tiles")
       sh.Fill.UserPicture "C:\Users\alband\Documents\ProDT\C4\C42.jpg"
    End Sub

    sh is just a variable to reference the Shape object.

    If you want to look at your objects - set a breakpoint in your code and look at the Locals window (from menu View-Locals).

    Nadia

     

    • Proposed As Answer by NadiaSz Monday, June 28, 2010 12:33 AM
    • Marked As Answer by alband Monday, June 28, 2010 8:58 AM
    •  
  • Sunday, June 27, 2010 8:51 PM
     
     

    Succes!!

    Thank you so much for the help! I just copied the extra across and it works. I've now multiplied the process and have got two 360 degree view of two isomers on my presentation!

    Thanks again for your help!

    David.

  • Monday, June 28, 2010 12:36 AM
     
     

    You are welcome!

    Glad that you delivered your presentation the way you wanted.

    Nadia