This document provides an overview of using VBA (Visual Basic for Applications) to automate tasks in Microsoft Word. It discusses the macro recorder's limitations and introduces the VBA editor. It also covers basic VBA concepts like objects, properties, methods, and events. Finally, it shows how to add user interfaces, loops, and conditional logic to macros to make them more intelligent and customizable.
10. Macros start with “Sub” Comments added automatically Actions performed by the user Macro name The VBA Editor: Inside the blue monkey Macros end with “End Sub”
introduction to the Visual Basic for Applications functionality that ships with Microsoft Office products. VBA taster session _______________________
_______________________
_______________________
_______________________
Cannot handle errors - for example, if a macro changes the border colour of a selected table and we run the macro when no table is selected we’ll get an error message. _______________________
The primary objective of the demos is to reinforce the concepts needed to start creating and editing macros. So, realism sacrificed for simplicity. All demos are variations of a simple macro called the blue monkey . _______________________
_______________________
_______________________
_______________________
Comments are ignored when the code is run. When you start editing and creating macros it is good practice to add comments throughout the code. You do this by starting the line with an apostrophe. Note the value for bolding is “wdToggle”. This relates to the limitation– the macro recorder guesses the user’s intentions. It assumes when I clicked the B button on the toolbar while creating macro my intention was to toggle the state of the button rather than to apply bold to the selected text. So, now if I were to click the B button before running the macro the code would toggle the bold off and the word “monkey” would be in regular font. This is not what I wanted. To overcome this I am going to edit the macro. _______________________
First I am going to show you the problem… >Side by side, bold turned off, F8 to run macro. Now bold turned on and run macro again. Now I am going to fix this by changing the selection.font.bold setting. >fix and rerun macro _______________________
_______________________
Last bullet point….The hierarchy is known as the Object Model. Each MS Office application has a different object model. _______________________
You can go a long way without ever needing to create new classes. _______________________
_______________________
This slide shows an example of a method and a property for the object called “Selection”. The Blue Monkey macro code uses this object and in fact the macro recorder often bases its code on this object. Typetext is an action that the object performs. It means it places the text on the page. Color is a property of the selection that can be changed. _______________________
Some objects have events associated with them. Example…the “Document” object can be opened so it has an event called “open” associated with it. VBA makes it particularly easy to access events for the Document object. It does this using the ThisDocument file. This is one of the standard files that I showed you earlier. If you click in the dropdown at the top right you will see a list of the most common event types for the Document object. So you just need to choose the event that you want to trigger your code and then add the code. _______________________
There are various ways to get help with creating macros… Note that Methods and properties are collectively referred to as “members” in the Object Browser and help file. _______________________
_______________________
_______________________
You can add all sorts of controls to a form. It’s easy…you just drag, drop and resize them… _______________________
So now the blue monkey is going to ask us a question…. >Create userform and add code to Thisdocument. Reopen doc. _______________________
There are many ways of adding intelligence to a macro. I am going to talk about two of those: looping and applying conditions.
So we’ve seen that when we record a macro it can set the properties of an object and makes that object do something. Now suppose we want to make that object do something repeatedly. If we had all day to spare we could just record a much longer macro where we repeat tasks over and over again. However, the VB Editor gives us an easy way to repeat a segment of code using a technique called looping . _______________________
So now we’re going to code the blue monkey to repeat itself… >Open yes button code and add For x = 1 to 10, next x …. >Reopen doc ______________________
Now let’s suppose we not only want to repeat the code but we also want to make a change each time the code runs. X is already incrementing by 1 with each loop so we’re going to take advantage of that to increment the point size in the code in the loop. When x is 1 we add 2pts. When x is 2 we add 4pts Etc _______________________
So now I am going to add the code for increasing the font size… >Open yes button code and add change font setting to 24 + 2*x , Reopen the doc…show increasing font size _______________________
Now suppose we only want a segment code to be run in certain conditions. There are various ways to do this. One is with an IF…THEN…ELSE construction. For this example, we only want to run the blue monkey macro if the document filename is Blue.docm…
So now I’m going to edit the macro so that the window only pops up if the document has the name blue.docm. >Add the following to the code in the ThisDocument and reopen If ActiveDocument.Name = "Blue.docm" Then … Else: MsgBox (“Rename the document to Blue.docm") End If >Reopen doc. >Close doc and rename to Blue.docm >Reopen doc. _______________________
_______________________
_______________________
Actually blue monkeys do exist… in Tanzania! _______________________