How to program with Microsoft PowerPoint

 How to program with PowerPoint!

When you hear about Microsoft PowerPoint, you instantly think about a presentation tool which is used for presenting statistics etc. But what you don't know, is that powerpoint can be used for so much more than just presenting! You can everything from animations, to small games!

In this blog post, I will show you how you can use PowerPoint macros to move an object with on-screen buttons!

End result:













I also left some challenges on the bottom of the page, so you can test your skill when you're done!


Step 1


Once you're created your PowerPoint document, go ahead and create a new blank slide with nothing on it.

Step 2




Now create a basic layout like this. The box is what is going to move, and the arrows will be the control buttons.

Step 3




Open the "Selection Pane" and rename your objects to make it easier. I named my box as "MovingBox", and arrows as "RightArrow" and "LeftArrow". If you want to follow my steps exactly, name them the same.

Step 4














Now head over to the "Developer" tab, and click on "Macros". Now type in anything in the name field (as this will be removed later), and click "Create". If you don't have the developer tab, follow these steps:
Click "File", and then "Options" on the bottom of the page. Then open the tab "Customize Ribbon", and tick off the checkmark next to "Developer".

Step 5











When you click "Create", you will be greeted with a new window. This is where you do the programming. Here you will be using the "VBA" as your programming language.

Step 6













Remove everything that already was there. Then type in this:




Function getRectangle() As Shape
Set getrectangle = ActivePresentation.Slides(1).Shapes("MovingBox")
End Function

The number "1" is the number slide you have the box on. So if you're box is on the second slide, change it to "2". With the "Set getRectangle" command, we are getting the box object and placing it in the variable "getRectangle".


Step 7






















Now we will write some code that will find the box's distant to the left "wall". Write:


Sub findShapePos()
Set rec = getRectangle
Debug.Print "Shape Left: " & rec.Left
End Sub


If you press the green "Play" button at the top, you should see the distant to the left "wall" in the "immidiate" tab below. If you don't have the immidiate window, press "ctrl + g".
If you for some reason don't see the results in the window, try doing the steps again.


Step 8














Now we will create the code that actually moves the box when we press the buttons. Write in this code:


Sub moveRight()
Set rec = getRectangle
rec.Left = rec.Left + 1
End Sub

Sub moveLeft()
Set rec = getRectangle
rec.Left = rec.Left - 1
End Sub


Here we are setting the new distant to the left wall to be 1 more / less than the old one. (newDistance = oldDistance + / - 1).


Step 9












Now go back to the PowerPoint document. Click on the Right arrow, and go to the "Insert" tab. From there click "Action". Now choose the "Run macro" option, and choose "moveRight".
Now do the same with the left arrow, but choose "moveLeft".

Now if you press "f5" to presentate your slide, you can click on the buttons to move the box. If you want the box to move faster or slower, you can simply change the value by how much the new distance should be.


Here are some challenges to test your skill:

  1.  Try to get the box to move faster. What value do you need to change?
  2.  Add up and down arrows aswell, and try to make them work. (Hint: You can only find distance from the Left wall and Top wall in VBA code.).


Kommentarer

Populære innlegg fra denne bloggen

How to pixelate any image in Adobe Illustrator

GitHub Copilot review

How to create a Vignette effect in Photoshop