>GRSoftware >VBA Tutor >Newsletter Tutorials >Tutorial 2 >Tutorial 4

VBA TUTOR NEWSLETTER ~ TUTORIAL THREE:
Objects and their properties

In this project we will create a 'FUN' macro that will demonstrate the use of 'Objects' and their 'Properties'.

  1. Enter some text into WORD, then open the VB Editor.

  2. Start a new macro with the name 'funfont', then enter the text "Selection."
    Sub funfont()
    Selection.
    End Sub

  3. You will notice that as you entered the period '.', a dialog box will open with a list of all the available properties for the object you are using. Scroll down the list until you see 'Font' and double click on it. The property will be appended to your code.
    Sub funfont()
    Selection.Font
    End Sub

  4. Enter a period '.' again, and a dialog box will open with a list of properties for the 'Selection.Font' object. Scroll down the list until you see 'ColorIndex' and double click on it. The property will be appended to your code.
    Sub funfont()
    Selection.Font.ColorIndex
    End Sub

  5. If you entered a period '.' again, the dialog box will NOT open, indicating that you now have to assign some value to the object property. Instead, enter a equal sign '='. A dialog box will open with a list of 'values' for the 'Selection.Font.ColorIndex' object. Scroll down the list until you see 'wdBrightGreen' and double click on it. The value will be appended to your code.
    Sub funfont()
    Selection.Font.ColorIndex=wdBrightGreen
    End Sub

  6. Now repeat the above procedure to add some more changes to the selected font, such as 'animation' and increased 'spacing'. It should look like the following for example ... Sub funfont()
    Selection.Font.ColorIndex = wdBrightGreen
    Selection.Font.Animation = wdAnimationSparkleText
    Selection.Font.Spacing = 10
    End Sub

  7. To finish the project, use the 'with' statement to reference the object 'Selection.Font', rather that reiterate it on each line of code.

  8. GOOD LUCK!

NOTE: Just delete the new macro when you have finished with it.

YOU HAVE NOW LEARNT HOW EASY IT IS TO USE OBJECTS AND THEIR PROPERTIES. ( AND HOPEFULLY HAD SOME FUN AS WELL! )


HOPEFULLY, THESE CODE EXAMPLES WILL ADD TO YOUR ABILITY TO BECOME A VBA POWER USER!
© 2000 Gary Radley