Header
PowerPoint tips, hints and tutorials that will change your presentations for ever!

INDEX

 

Jigsaws
Sounds
Video
Custom Shows
vba code
NaviSlides
Games for teachers
Bullets
Triggers
Security
Flash Cards
Multiple Instances
PowerPoint 2007
Mail Merge
Random events
Animation
Hyperlinks
Set spellcheck language


Home buttonTutorial buttonContact buttonProducts button


Writing VBA Lesson Two

Hopefully Lesson one worked OK but it wasn't very useful. This lesson will be SLIGHTLY more useful and introduce you to variables. It makes some important points about using variables so read on even if you think you understand variable use!

Using the code from Lesson One edit it to look like this. Notice how as you type PowerPoint suggests words in a dropdown menu. This is called intellisense.

Use a variable 

We have used a VARIABLE called iSlideCount to store the value of the number of slides in the current presentation and then used a message box to print out the answer. We could make the message more interesting by combining a statement with the variable using & like this:

Improved message

We have used our first variable BUT we have also created code that most programming languages will not allow. Office vba allow you to use a variable without declaring it and this is BAD. Declaring simply means telling the PC I'm going to use a variable called xyz in my code BEFORE you use it. So before we continue lets make PowerPoint obey this rule and insist on variables being declared.

Still in the vb editor go to the TOOLS menu and choose OPTIONS. Tick "Require Variable Declaration". Also UNTICK "Auto Syntax Check". We will explain this one later! From now on PowerPoint will type Option Explicit at the top of your modules and INSIST that variables are declared.

Option Explicit

So - How do we do that. lets go back to the code and add this line.

Variable declaration

We have told the PC that we are goind to use the variable iSlideCount in this Sub AND that its type is INTEGER. An INTEGER is a whole positive or negative number. It cannot be a decimal or fraction. If you try to make it one it will round down to the nearest whole number. More on variable types in the next lesson. Notice that the name starts with i this is not required but is good practice and reminds you that this is an i(NTEGER).

Why Should We Insist on Variable Declaration?

First your variable name will be added to intellisense.

More important - consider this code without Option Explicit enabled.

Bad code

It should warn you if the presentation has no slides BUT it ALWAYS gives the warning even if you have lots of slides. The mistake is easy to spot in this short sub but it wouldn't be if you had 1000 lines of code. There's a spelling error in the second variable. PowerPoint isn't smart it just thinks "This is a new variable and therefor equal to zero!

If you enable "Require Variable Declaration" (note it will not take effect until the next module is opened - manually type in Option Explicit to test) PowerPoint instead say "This variable is not declared warn the programmer there's an error. Much easier to debug.

Error

Next Lesson "Variable Types"

 
 

Back to the Index Page

POWERPOINT BLOG

Articles on your favourite sport

Free Microsoft PowerPoint Advice, help and tutorials, Template Links
This website is sponsored by Technology Trish Ltd
© Technology Trish 2007
Registered in England and Wales No.5780175
PowerPoint® is a registered trademark of the Microsoft Corporation