Etudes for Microsoft Word Programmers
Etude 1.1. Text Fit
Font in printing, sometimes called Fount, a complete set of type of any one size, with all the usual points and accents; a font consists of about 100,000 characters.
At the time of final text formatting it is usually needed to change the size of font or character spacing for one or several paragraphs. In this etude we also study some “how to” – how to create Microsoft Word template, how to open Microsoft Word template, how to write a simple VBA macros and how to create a simple toolbar.
At least beginning with Microsoft Word 2003 version there exist the built-in commands "Grow Font (Ctrl+>)", "Shrink Font (Ctrl+<)", and also "Grow Font 1 Pt (Ctrl+])" and "Shrink Font 1 Pt (Ctrl+[)", which are the best for the creation of your first template. Creation of personal versions of the built-in commands gives the opportunity of their change and improvement in future.
The operations “Creation of an empty template”, “Opening of a template”, “Creation of a module” described below are aimed at the readers who don’t know the first thing about VBA and Visual Basic Editor.
The purpose of these operations is that a reader who has no regular access to the i-net could understand that a file of the Microsoft Word template with the extension .DOT contains the VBA modules and toolbars. So in the modules there is the VBA code, macros, forms and classes, i.e. the source text of the program.
If you downloaded the "AhTextFit.dot" template and you know the VBA, you may pass over to the experiments with the templates, studying the code and the tasks at the end of the etude.
Create a new empty Microsoft Word document and push “Save”. In the dialog “Save As” chose at first “Save as Type” the extension “Document template (*.dot)” and then choose the necessary folder and name for the file.
To open a template use the “Open” menu command or pick it up from the list of the recently opened files in Microsoft Word “File” menu.
Open the created template (the file with the extension .DOT) and go to the Visual Basic Editor (choose the Microsoft Word menu command "Tools\Macro\Visual Basic Editor" or push Alt+F11). With the help of the context menu create a new module in the project (in the opened template).
In the properties editor change the name of the module from “Module1” to “AhTextFit”.
Transfer (copy here and paste there) the macros given below to the text of the module.
Simple operations of text formatting are implemented in the template “AhTextFit.dot”. The toolbar “AhTextFit” is the user’s interface for the basic formatting operations, like changing of the font size and spacing.
The basic function which changes the font size is AhTextFitFontSizeChange. It also handles errors. The functions AhTextFitFontInc and AhTextFitFontDec just call the function AhTextFitFontSizeChange supplying the necessary arguments.
Similarly, the basic function which changes the character spacing is AhTextFitFontSpacingChange. It also handles errors. The functions AhTextFitSpacingInc, AhTextFitSpacingDec and AhTextFitSpacingNormal just call the function AhTextFitFontSpacingChange supplying the necessary arguments.
The full source code of the module AhTextFit which can be found in the template “AhTextFit.dot” is given below.
If the selected text consists of the symbols of different size, the Selection.Font.Size parameter is not defined (and the Size property returns wdUndefined constant). In such case it is possible to change the font size of every given symbol in the selection, but it is not always acceptable. The AhTextFitSpacingChange function's behaviour depends on the cbProcessEachCharIfWdUndefined value, which is defined at the beginning of the module. If the value is True then the function changes the font size of each symbol. Otherwise the error message is displayed.
Now we pass over to the creation of the simplest user’s interface – a toolbar. Our goal is the call of the created (or built-in) macros with the help of one click on the toolbar button. Use the menu “Customize” to create a new toolbar with the name “AhTextFit”.
Add buttons to the toolbar. Assign the corresponding macros to the buttons, add icons. The text of the buttons initially contains the full long name of the function (something like ProjectName.ModuleName.MacroName), so change the name to the shorter and easier variant. The toolbar is ready to be used.
The views of the toolbar are given below: the floating toolbar, the toolbar among other Microsoft Word toolbars, the “icons only” toolbar.
Short description of the buttons of the toolbar “AhTextFit” is given in the table below.
Copy the template “AhTextFit.dot” (it should have been saved or just exists) to the Microsoft Word Startup folder and start or restart Microsoft Word. If loading of the template is not forbidden and the necessary macros security level is set then the created template will be loaded automatically and the toolbar will be available from any document. In other words, copying the template into the Microsoft Word Startup folder makes it a global template, or a plugin.
Open any document and test the toolbar “AhTextFit” buttons. Don’t forget to select one or several paragraphs.
After successful testing the template "AhTextFit.dot" is ready for use.
The brief prompt about the toolbar buttons looks like this:
If you press the button “Increase Font Size” then the font size of all selected paragraphs is increased by 1 point. If the selected text contains fonts of different size then the result is dependent on the value of cbProcessEachCharIfWdUndefined constant.
If the value of cbProcessEachCharIfWdUndefined is True then the Program increases the font size even if the selected text contains fonts of different size.
If the value of cbProcessEachCharIfWdUndefined is False and the marked text contains fonts of different size then the following error message appears.
The text “before” and “after” increasing the font size is given in the picture below.
In case of the attempt to make the font size very big or to make it equal to zero the error message like this appears:
The attempt to set font size zero manually provokes a message like this:
If you press the button “Increase Font Size” then the font size of all selected paragraphs is decreased by 1 point. If the selected text contains fonts of different size then the result is dependent on the value of cbProcessEachCharIfWdUndefined constant.
If the value of cbProcessEachCharIfWdUndefined is True then the Program decreases the font size even if the selected text contains fonts of different size.
If the value of cbProcessEachCharIfWdUndefined is False and the selected text contains fonts of different size then the error message appears.
If you press the button “Increase spacing” then the space between symbols in all selected paragraphs increases by 1 point.
A text with different space size (spacing) is given below:
If the selected text contains different character spacing sizes then the result is dependent on the value of cbProcessEachCharIfWdUndefined constant.
If the value of cbProcessEachCharIfWdUndefined is True then the Program increases the spacing even if the selected text contains different character spacing sizes
If the value of cbProcessEachCharIfWdUndefined is False then the error message appears.
If you press the button “Decrease spacing” then the character spacing in all selected paragraphs decreases by 1 point. If the selected text contains different character spacing sizes then the error message appears.
If you press the button “Normal Font Spacing” then the normal spacing is set for all selected paragraphs.
The process of the template creation and its opening, the process of the module creation and the toolbar creation were briefly described in this etude. In the second chapter of the book, which is entirely dedicated to the menus and toolbars, these questions are studied in detail.
The simple operations of text formatting like changing the font size and changing font spacing were also studied in this chapter. The toolbar “AhTextFit” was created. It represents a user’s interface for the simplest operations of text formatting.
Etudes for Microsoft Word Programmers. Etude 1.1.
otherwise noted, all materials on this site are