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.
-----SIDEBAR-----
If anything at all is not clear in these tutorials, or you’re using an earlier version of Word and something more than trivial is appearing or is not appearing in the code you see here, please let us know so we can correct this, for you, and for others. And especially, if something goes over your head because we have assumed something we shouldn’t have. (There will be a link to a form here soon)
Also, please don't try to understand every detail you'll see in some of the resulting code. The purpose of our recording macros is to see the ball park at 30,000 feet and to show you how this it can suppliment your efforts in code you write.
-----SIDEBAR-----
So that we’re on the same page, please open a new document to work in. OK, turn on the macro recorder. Go to Tools>Macro>Record New Macro… , remember the name of this macro that appears in the dialog window and press Enter. If no one has ever created macros on your computer, the name should be Macro2, since we created Macro1 in the previous tutorial. If it is not Macro2, just remember its name.
1. Now, type the letters (without the quotes) Hello World (no period either). Because Word is in a recording mode, we aren’t able to use the mouse as we could normally to select the text we have typed. So hold down either the left or right Shift key and press the left arrow 11 times. This should highlight, one by one, the letters you typed.
2. Go to the Format (menu)>Font. At the very top of the Font tab that now displays, change the font to the next larger size available. (If 11 is highlighted, select 12 or whatever comes after the 11 for the font style in use). Then click OK.
3. Press the End key to deselect "Hello World" and move the cursor to the end of the line.
4. Press the Enter key twice and then type the word Hello.
5. Go to the Edit menu and click "Select All."
6. You should see a dropdown (combo box) in the toolbar for changing the font size. Change it again to the next larger font size. If you don’t see it and don’t know how to get it to appear on the toolbar, don’t worry, we’re going to stop the recorder now.
7. Stop the recorder with the stop button, or go to Tools>Macro>Stop Recorder.
Now we’ll examine how this is done though coding. Don’t be alarmed, since the recorder wrote the code there are many more lines than there would be if you had written it, as you’ll see.
Open the Editor, Go to Tools>Macro>Visual Basic Editor.
Scroll down if you don’t see the code block of the macro that you remember the name of, and find Macro2(). It should look, basically, if not exactly like this:
Sub Macro2()
'(these are comment lines, they start with an apostrophe and are not executed)
Selection.TypeText Text:="Hello World"
Selection.MoveLeft Unit:=wdCharacter, Count:=11, Extend:=wdExtend
With Selection.Font
.Name = "Times New Roman"
.Size = 12
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.SizeBi = 11
.NameBi = "Times New Roman"
.BoldBi = False
.ItalicBi = False
End With
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Hello world"
Selection.WholeStory
Selection.Font.Size = 14
End SubThe first 2 lines of the actual code , not including the code block header (Sub Macro2()) are:
Selection.TypeText Text:="Hello World"
Selection.MoveLeft Unit:=wdCharacter, Count:=11, Extend:=wdExtend
Again, as with our first macro, the recorder also added something we would almost never need to be concerned about. Namely, Text:=, Unit:=, and other uses of the ":=" notation. To see why not, put the cursor (by clicking your mouse or by navigating with the arrows) after the "t" at the end of the word "MoveLeft" and before the space that follows it. Now, press the spacebar. You should see the following:

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"
Selection.MoveLeft wdCharacter, 11, wdExtend
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:
With Anything
.Something
.Nothing
End With
We could write it as:
Anything.Something
Anything.Nothing
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.
In the next three lines...
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Hello world"
we see that we pressed the Enter twice, which word calls TypeParagraph.
Then...
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.
Go to the next tutorial: "Using VBA in Word"
Return from Running a Macro Using VBA to "Free VBA Tutorials"
Return to our "Home Page"

|