Hello World in VBA
Hello World in VBA. The phrase, "Hello World" is used in many beginning programming tutorials. Usually, it is displayed as a popup message by writing the code to do it. In this lesson we’ll continue with the macro recorder to generate code that types for us, and in the next lession we’ll add a message box which is a small popup message.
Hello World in VBA - Part 1Hello World in VBA. The phrase, "Hello World" is used in many beginning programming tutorials. Usually, it is displayed as a popup message by writing the code to do it. In this lesson we’ll continue with the macro recorder to generate code that types for us, and in the next lession we’ll add a message box which is a small popup message.
If you had typed a new line with Selection.MoveLeft and then pressed the spacebar, Word would have prompted you that it is expecting arguments for the Unit to move, in this case, a character at a time, the Count, 11 characters (we highlighted the text by pressing the Shift key and the left arrow 11 times, and the Extend arguement, here it is being told to hold down the Shift key and extend the selection (not just move the cursor). This is how we highlighted Hello World without the aid of the mouse. As confusing as this may look at this point, I’m only trying to make two points.
1. Not to worry about named arguments (like Unit:=) because VBA will prompt you as to what arguments it needs. And at the times we’ll need to know this, VBA will usually tell us the options we can choose from (unlike in this instance).
2. To review how creating a space after most keywords will show a prompt. This, by the way, is true of the period after a Keyword (if it legitimate to put a perioid there).
Also, you won’t have to remember words like wdCharacter and wdExtend. You will be creating commands (statements) like these with the recorder. You don’t need them when doing most of any customization you will do. If you're curious, named arguements give you the possibilty of supplying arguements out of the expected order.
So let’s delete the named arguments. Like this:
Selection.TypeText "Hello World"
The next lines between "With Selection.Font" and "End With" have two points of interest.
1. You don’t need to work with this type of block (With, End With) at all. All the lines within the With block start with a period and this is just a shorthand way of writing each line within the block as: Selection.Font.Name = "Times New Roman" and so on for all the lines in the block. So if we had a With block:
We could write it as:
2. If you remember, we only changed the size of the font, but Word has recorded this as if we had changed the Bold, Underline, and everything else about the font. Since it virtually doesn’t take any time for Word to print this code and you could have changed all those options when the Font dialog was open while you were changing the size, Word was programmed in itself to find this easier/quicker than following you around in the dialog box to see what you are doing. When we changed the font again using the toolbar just before we stopped the recorder, Word wrote the same change with one line of code.
Hello World in VBA - Part 2
In the next three lines...
we see that we pressed the Enter twice, which word calls TypeParagraph.
Selection.WholeStory Selection.Font.Size = 14
we see that we chose Select All from the edit menu, which word calls WholeStory and then changed the font size again to a larger size.
In the next lesson, I’ll show you how you can record a similar macro that you can use if you surf to a site with very small print and would prefer to quickly paste and format them to a larger font with the click of a mouse.
We’ll also explore some features of the Editor.
This site is powered by Site Build It!. If you enjoy it, please check out the
Site Build It homepage to learn more and on how to build a success-guaranteed site with no technical skills.
Go to the next tutorial: "Using VBA in Word"