Creating MS Word Documents (Part 2)
This is the second in a 4-part series concerning creating MS Word documents in LotusScript. In the first tip, we talked about the basics of creating a Word document and adding text to it. In this tip, we'll get a bit more advanced with some formatting options, and the last two tips we'll talk about updating headers and footers and some other advanced topics. Now that we know how to create a Word document and add some text, we need to know how to format that text. Let's look at making the text either bold or italic. This is done with two different subroutines. The subroutines take a range (the return value from the AppendTextToDoc function) and a True/False value to indicate if the range should be bolded (or italicized) or not bolded (or not italicized). The subroutines are pretty straightforward, once you know the syntax as described on the MSDN web site.
Sub SetBoldOnOff(range As Variant, isOn As Integer)
If Not range Is Nothing Then
If isOn Then
range.Bold = True
Else
range.Bold = False
End If
End If
End Sub
First, we make sure we have a valid range. Then we change the Bold property of the range to the appropriate value, based on what was passed in.
Sub SetItalicsOnOff(range As Variant, isOn As Integer)
If Not range Is Nothing Then
If isOn Then
range.Italic = True
Else
range.Italic = False
End If
End If
End Sub
For italics, it works very similarly, with the only exception being the range property that is updated.
Underlining is a bit different. There are different kinds of underlining that are possible. These are defined in the MSDN documentation with constants like wdUnderlineSingle or wdUnderlineDouble. These constants are not available to LotusScript. So how do you know what values to use? The easiest way we have found is to use the MS Word Macro editor. Each version of Word might be different, but figure 1 shows how to open up the macro editor. The easiest thing to do is create your own macro. In there, just put in statements listing out the variables you want to check, as seen in figure 2. You can then go to the Debug menu and choose Step Into to start the debugger. While you are debugging, you can mouse over the constant and the actual value will pop up, as seen in figure 3. Using this technique, we can find out that wdUnderlineSingle is a value of 1, and wdUnderlineDouble is a value of 3.
Using that knowledge of the constants, we can build a subroutine that will underline the text in a range. Instead of having our own "internal" values for what kind of underlining the developer may want to do, we chose to instead implement a couple of subroutines similar to the subroutines for bolding text. These are called SetUnderlineOnOff and SetDoubleUnderlineOnOff.
Sub SetUnderlineOnOff(range As Variant, isOn As Integer)
If Not range Is Nothing Then
If isOn Then
range.Underline = 1 ' wdUnderlineSingle
Else
range.Underline = 0 ' wdUnderlineNone
End If
End If
End Sub
Sub SetDoubleUnderlineOnOff(range As Variant, isOn As Integer)
If Not range Is Nothing Then
If isOn Then
range.Underline = 3 ' wdUnderlineDouble
Else
range.Underline = 0 ' wdUnderlineNone
End If
End If
End Sub
Font information can also be set. Internally, we set up several functions so the developer using the code isn't restricted. Here, we'll go over the "base level" functions that set the font face and font size. You can also write a "wrapper" function that sets both the face and size by calling the lower level functions.
Sub SetFontFace(range As Variant, fontName As String)
If Not range Is Nothing Then
range.Font.Name = fontName
End If
End Sub
This is a pretty straightforward subroutine, but again it is one that hides the underlying MS Word structure from the developer. Like I said earlier, we have "wrapper" functions that make it easy for the developer to set the font information in one pass. The subroutine for setting the font size is very similar:
Sub SetFontSize(range As Variant, fontSize As Integer)
If Not range Is Nothing Then
range.Font.Size = fontSize
End If
End Sub
We also have a function for setting the font color, but I won't disclose that here. The only thing I'll point out about the color is to be careful with the order. The value is a string in "Blue/Green/Red" format. It is not the "Red/Green/Blue" (RGB) format that you're used to on the web. For example, if you would like to have a yellow color (full Red plus full Green), instead of having hex value "FFFF00", you would need to use hex value "00FFFF" in MSWord.