Etudes for Microsoft Word Programmers

Home ] Up ] Etude 1.1. Text Fit ] Etude 1.2. Symbol Case ] Etude 1.3. Finding and Painting Hyperlinks ] Etude 1.4. Styles ] Etude 1.5. Inserting Formatted Text ] Etude 1.6. Text in the Column. ] Etude 1.7. Formatting Headline. ]


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.

http://www.websters-online-dictionary.org/definition/Font

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.


Letter Spacing

Adjusting the average distance between letters in a block of text to fit text into the given space or to improve legibility.

 


Linespace
 Leading

The amount of space added between lines of text to make the document legible or the total height from baseline to baseline of rows of text. The term originally referred to the thin lead spacers that printers used to physically increase space between lines of metal type. Most applications automatically apply standard leading of 120% of the font's point size.

 


Template
Plug-in

Computer program that interacts with a main (or host) application (a web browser or an email program, for example) to provide a certain, usually very specific, function on demand.
 Microsoft Word plug-ins (add-ins) are global templates with DOT extension (usually located in the Startup folder) and special dynamic libraries with WLL file extension.

 

Resources

Template "AhTextFit.dot", 21 Kb, ZIP [download].

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.

Creating an Empty Template

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.

Opening a Template

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. 

Double click of the left mouse button on the name of the template file forces Microsoft Word to create a new document based on the chosen template. You won’t need this operation while reading this book because the topic of the creation of new documents based on the given templates is not investigated in this book.

Creating New Module

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.

Just in case I want to remind you that all the above-given operations are necessary only if you don’t have the template. In other words, if you read the text on the paper and youê computer is out there or is not connected to the Internet.

Template "AhTextFit.dot"

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.

Constants

'
' File AhTextFit.dot|AhTextFit
'
' Etudes for Microsoft Word Programmers.
' Etude 1.1. Text Fit.
' First published on http://www.transcriber.ru
'
' © 2000-2007. Evgeny Akhundzhanov. All rights reserved worldwide.
'
Option Explicit
'
' Constants
'
Private Const ciFontSizeStep As Long = 1
Private Const ciFontSpacingStep As Long = 1
Private Const cbProcessEachCharIfWdUndefined As Boolean = False

Error Messages

'
' Messages
'
Private Const cErrFontSizeUndefined As String = "Font size = wdUndefined."
Private Const cErrFontSpacingUndefined As String = "Font spacing = wdUndefined."
Private Const cErrFontSizeError As String = "Cannot change font size."
Private Const cErrFontSpacingError As String = "Cannot change font spacing."

Subroutine AhTextFitAbout

Sub AhTextFitAbout()
MsgBox "Etudes for Microsoft Word Programmers." & vbCrLf & _
    "Etude 1.1. Text Fit." & vbCrLf & _
    "http://www.transcriber.ru" & vbCrLf & vbCrLf & _
    "Template AhTextFit.dot - Font size and spacing Fit" & vbCrLf & vbCrLf & _
    "Parameters are as follows:" & vbCrLf & _
    "Font Size Step = " & ciFontSizeStep & vbCrLf & _
    "Font Spacing Step = " & ciFontSpacingStep & vbCrLf
End Sub

Subroutine AhTextFitFontSizeChange

'
' AhTextFitFontSizeChange
'
Private Sub AhTextFitFontSizeChange(ByVal dwStep As Long)
If Documents.Count = 0 Then Exit Sub
 
On Error GoTo ErrorLabel
With Selection.Font
    If dwStep = 0 Then
        ' just do nothing
    Else
        If .Size = wdUndefined Then
            If cbProcessEachCharIfWdUndefined Then
                Dim dwChar As range
                On Error Resume Next
                For Each dwChar In Selection.Characters
                    dwChar.Font.Size = dwChar.Font.Size + dwStep
                Next dwChar
            Else
                MsgBox cErrFontSizeUndefined
            End If
        Else
            .Size = .Size + dwStep
        End If
    End If
End With
Exit Sub
ErrorLabel:

    MsgBox cErrFontSizeError
End Sub

Subroutine AhTextFitFontInc

'
' AhTextFitFontInc
'
Public Sub AhTextFitFontInc()
    AhTextFitFontSizeChange ciFontSizeStep

End Sub

Subroutine AhTextFitFontDec

'
' AhTextFitFontDec
'
Public Sub AhTextFitFontDec()
    AhTextFitFontSizeChange -ciFontSizeStep
End Sub

Subroutine AhTextFitSpacingChange

'
' AhTextFitSpacingChange
'
Private Sub AhTextFitSpacingChange(ByVal dwStep As Long)
If Documents.Count = 0 Then Exit Sub
 
On Error GoTo ErrorLabel
With Selection.Font
    If dwStep = 0 Then
        .Spacing = 0
    Else
        If .Spacing = wdUndefined Then
            If cbProcessEachCharIfWdUndefined Then

                Dim dwChar As range
                On Error Resume Next
                For Each dwChar In Selection.Characters
                    dwChar.Font.Spacing = dwChar.Font.Spacing + dwStep
                Next dwChar
            Else
                MsgBox cErrFontSpacingUndefined
            End If
        Else
            .Spacing = .Spacing + dwStep
        End If
    End If
End With
Exit Sub
ErrorLabel:

    MsgBox cErrFontSpacingError
End Sub

Subroutine AhTextFitSpacingInc

'
' AhTextFitSpacingInc
'
Public Sub AhTextFitSpacingInc()
    AhTextFitSpacingChange ciFontSpacingStep

End Sub

Subroutine AhTextFitSpacingDec

'
' AhTextFitSpacingDec
'
Public Sub AhTextFitSpacingDec()
    AhTextFitSpacingChange -ciFontSpacingStep
End Sub

Subroutine AhTextFitSpacingNormal

'
' AhTextFitSpacingNormal
'
 Public Sub AhTextFitSpacingNormal()
     AhTextFitSpacingChange 0
 End Sub

Constant wdUndefined

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.

Toolbar "AhTextFit"

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.

Changing the macros names which are linked with toolbar buttons is not recommended. The full name of the macros includes the name of the module and Microsoft Word saves it somewhere. That is why any change of the module name after the toolbar creation leads to its complete failure. To transfer macros and toolbars from one template to another use the special dialog of the menu "Tools\Macro\Macros\Organizer".

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.

Button

Macros

Description

Help

AhTextFitAbout

Brief prompt of the buttons of the toolbar.

Increase font size

AhTextFitFontInc

Increase font size.

Decrease font size

AhTextFitFontDec

Decrease font size.

Increase spacing

AhTextFitSpacingInc

Increase font spacing.

Decrease spacing

AhTextFitSpacingDec

Decrease font spacing.

Normal spacing

AhTextFitSpacingNormal

Normal font spacing.

 

Creation of toolbars will be described in detail in the chapter “Menu and toolbars”. That is why in this etude it is given briefly.

Testing

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.

Help

The brief prompt about the toolbar buttons looks like this:

Increasing Font Size

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:

The font size can vary from 1 to 1638 units.
Or from 1/72 to 22.75 inches.
Or from 0.035 to 57.785 centimeters.

Decreasing Font Size

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.

Increasing Font Spacing

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.

Decreasing Font Spacing

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.

Normal Font Spacing

If you press the button “Normal Font Spacing” then the normal spacing is set for all selected paragraphs.

Summary

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.

Tasks

Task 1

Change the macros in order for the program to change the size even if the selected text contains different font size or different font spacing.

Prompt: Set the value True for cbProcessEachCharIfWdUndefined constant and test the result. Experiment with both variants and decide, which you like more.

 

Task 2

If you like more the built-in commands "Grow Font (Ctrl+>)", "Shrink Font (Ctrl+<)", "Grow Font 1 Pt (Ctrl+])" and "Shrink Font 1 Pt (Ctrl+[)" than those, given in this etude, just change the text of the corresponding macros. Do it on your own. Don’t be shy to experiment. If anything goes wrong you can always return to the primary variant of the template.

 

Task 3

Besides character spacing there is also line spacing. Add the corresponding macros and toolbar buttons for changing line spacing. Do not forget that the already existing macros is called by the button click.

 

Task 4

If you still want to experiment with this template, study all properties of the Font Object. And then think of all built-in functions with it. Write the list of the necessary improvements with the template and step-by-step make it true.

In the code, given in this etude you met the words “Documents”, “Selection” and “Range”. In the next etude there is the hint of what it is.

 


Etudes for Microsoft Word Programmers. Etude 1.1.


Unless otherwise noted, all materials on this site are
© 2000-2009 Evgeny Akhundzhanov, All Rights Reserved Worldwide
Microsoft is in no way affiliated with, nor offers endorsement of, this site.
www.transcriber.ru | E-mail the Author