>GRSoftware >VBA Tutor >Newsletter Tutorials >Tutorial 1 >Tutorial 3

VBA TUTOR NEWSLETTER ~ TUTORIAL TWO:
Improving the standard Menu Items.

In this project we will create a macro based on a recorded standard Menu Item, with the code altered to improve its function.

  1. If you have not already added the "Grow Font 1 Pt" Menu Item to your Formatting Toolbar, do so by using the "View|Toolbars|Customize" Menu Item. [It is a 'Format' command.] Then add some text to a blank document.
  2. Highlight some or all of the text you have entered, then start the macro recorder by selecting "Tools|Macro|Record New Macro", Accepting the default macro name. eg."Macro1".
  3. Click on the "Grow Font 1 Pt" Menu Item.
  4. Stop the macro recorder.
  5. Go to "Tools|Macro|Macro", and select the newly recorded macro, then click on the "Edit" button.
  6. In the VB Editor, you can see the code for the recorded macro. It should look like ...

Sub Macro1()
Selection.Font.Size = Selection.Font.Size + 1
End Sub


NOW ... as in tutorial one, we are first going to get the user to enter some information before the action is carried out. What we are going to ask for is amount to grow the font, to replace the default of '1 Pt'.

The completed code should look like ...

Sub Macro1()
Dim Message, Title, Default, growSize, checkNum
GetSize:
Message = "Enter a positive value to increase the font size by."
Title = "Grow Font Size"
Default = 1
growSize = InputBox(Message, Title, Default)
checkNum = IsNumeric(growSize)
If checkNum Then
Selection.Font.Size = Selection.Font.Size + Abs(growSize)
Else
GoTo GetSize
End If
End Sub


Note the checking done to ensure that only a positive number is passed as the 'growSize' variable. It is first checked by the 'IsNumeric' inbuilt function. If is is not a number, 'checkNum' will be false and the procedure will loop back to again ask for a positive number using the 'goto' statement. If 'growSize' is a number, the 'Abs()' function is used to ensure that only a positive number is passed.
NOW ...
To use 'Macro1' as the default "Grow Font 1 Pt" Menu Item, it must be renamed to the same name as the standard "Grow Font 1 Pt", which is "GrowFontOnePoint". [ REMEMBER, to find the name of any Menu Item, use the procedure oulined in Issue 1 of this newsletter.]
Go and test the NEW "Grow Font 1 Pt" Menu Item. I think you will agree that it is a improvement.
NOTE: Just delete the new macro to restore the "standard" Menu Item.

YOU HAVE NOW LEARNT HOW EASY IT IS TO IMPROVE THE STANDARD MENU ITEMS.



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