Visual Basic for Applications is what I'm learning in school right now. It's a pretty simple language, but also a bit of a useless one. But, it's an interesting introduction to programing. I've actually been having a bit of fun with it.
So, what VBA is, is a language mostly used in scripting Microsoft Office. However, it is used for other programs as well.
What the class I'm in, and what this thread will try to do, is teach how to script Office to do things it's not meant to do.
VBA in office is a bit of a controversial topic, because there's some people who think its great and want to keep it in future versions of Office, others that think its horrid and a tremendous security risk, and 90% of the users who didn't even know it existed.
Its also a very old function of Office. This aspect of the software has not changed at all since Office 97 came out. The book we're using in class was written in 2000 and is still applicable today.
So, let's get started with some basics.
Obviously, you will need Microsoft Office. Just about any version will do, I'm using 2007 myself so everything here will be based on that.
First, we need to tweak some settings.
Go ahead and open up Word. Click the Office button, then "Word Options".
Once there, click the checkbox next to "Show Developer tab in the ribbon" and click OK. This will give you a new tab in all Office applications.
The first thing we'll do, is record a simple macro. A macro is a script that automates something.
Click the Developer tab on the ribbon, and in the Code group, you should see "Record Macro". Go ahead and click that, and click OK on the box that comes up.
You are now recording your actions within the document.
Now, type "This is my first macro"; hit "Stop Recording" on the ribbon.
Click "Macros" in the Code group on the ribbon, a box should come up showing what macros you have recorded.
Click on the macro we just made to select it, it should be named Macro1. Click "Edit" off to the right.
This should bring up a VB editor window and the code that generates the macro we just recorded.
Let's take a look at the macro.
Sub Macro1() ' ' Macro1 Macro ' ' Selection.TypeText Text:="This is my first macro" End Sub
So, what is going on here?
Sub Macro1()This indicates that we have a sub procedure, and that the name of that procedure is "Macro1".
' ' Macro1 Macro ' 'This is a comment, it does nothing. It tells whoever is looking at the code what it is. Comments can be anything, and I'll show you more later.
Selection.TypeText Text:="This is my first macro"This is what our macro is actually doing. It's typing text ("typetext") into the selection (where the cursor is), and the text it typed was "this is my first macro". Pretty simple, isn't it?
End SubEnd of the macro.
So, lets have some fun with this. In the VB editor here, we can tweak our macro to do other things.
Lets have it come up with a little message box saying it was successful.
The code for a message box is pretty simple. Start by entering the following:
Selection.TypeText Text:="This is my first macro" msgboxWhen you hit space after typing "msgbox", you will see a little help popup. So, lets continue on.
msgbox "Great Success!",After you put the , at the end, another list of options will come up showing what kind of buttons we want on our box. The default is "okonly", so we can simply put another comma here. I would encourage you to play around with the different options here and see what they do.
msgbox "Great Success!",,"woot"With this, "woot" will be shown as the title of our message box. This is optional of course.
We won't worry about the other options for now. Hit the enter key and review your code.
Sub Macro2() ' ' Macro2 Macro ' ' Selection.TypeText Text:="This is my first macro" MsgBox "Great success!", , "Woot" End Sub
To test it, simply hit F5 on your keyboard.
greatsucess.PNG 68.78KB 7 downloads
So, I'll leave that as our first lesson. I'll see how well this thread goes over before posting more.