Refining the macro to make it more useful

The text we want to insert should be at the end of the document rather than the beginning. So we need a way of working out where the end of the document is. One way is to ask Ability for the total number of characters in a document as follows:

ActiveDocument.Text.Count

So the following macro would always insert the text at the end of the document:

Sub LetterClosing

pos = ActiveDocument.Text.Count 

ActiveDocument.Text.Insert pos, "Hello from Ability" 

End Sub

Notice that we assigned a variable to count the number of characters in the document. Strictly speaking, it is not really necessary and we could have written it as a single line instead:

ActiveDocument.Text.Insert ActiveDocument.Text.Count, "Hello from Ability"

But the first way is easier to read (which is a good rule of thumb to apply when creating macros).

Supposing you had lots more similar commands you wanted to issue. Do you have to keep writing "ActiveDocument.Text" all the time? The answer is No. You can ask Ability to perform a series of tasks using the same "thing" (or you can call it object if you want). For example, the following macro gives identical results to the previous one:

Sub LetterClosing

With ActiveDocument.Text 

pos = .Count 

.Insert pos, "Hello from Ability" 

End With 

End Sub

There are some important points here. When using the With statement, the . (dot or period) must precede any items that refer back to the object being referred to. So it reads: "pos equals dot count" and "dot insert equals....". Also note that it is good practice to indent macro commands within "block" statements. This makes it easier to read.

Now let’s make the macro put some real text in.

Sub LetterClosing

With ActiveDocument.Text 

pos = .Count 

.Insert pos, vbCr & "Yours sincerely" & vbCr & vbCr & vbCr & vbCr & "Philip Roach" & vbCr & "Technical Manager" 

End With 

End Sub

What's happening! Imaging you were typing the text in real life - after the "Yours sincerely", you'd press the Enter key. You'd probably press it once (or twice) before you started "Yours sincerely" as well. This is what the vbCR does. It's short for "visual basic carriage return" and is a constant built-in to the VBScript language.

Also, the above example shows how to combine text strings using the "&" operator.

Having long strings of text combined in a single line as in the above example is not good practice (it's hard to read later). Here is a neater way of laying it out:

Sub LetterClosing

With ActiveDocument.Text 

pos = .Count 

.Insert pos, vbCr & "Yours sincerely" & String(4, vbCr) & _ 

"Philip Roach" & vbCr & _ 

"Technical Manager" 

End With 

End Sub

Instead of repeatedly chaining vbCr's together, we can use a built-in function (built-in to the VBScript language that is) called "String" that takes a character and repeats it any number of times. Also notice that the long line is split into 3 with an underscore used as a "continuation" character at the end of lines 1 and 2.