I'm continuing the discussion on how to create a word document. Now it's time to understand how to format text in Word;
This time I'll show how to write Bullet Lists into a word document.
First, read up on how to create a word document here:
http://www-10.lotus.com/ldd/ddwiki.nsf/dx/HowTo_Create_a_Word_Document_from_LotusScript
So, let's start with creating a blank Word document:
Dim objWord As Variant ' Holder for Word (application)
Dim objDoc As Variant ' Holder for the word document
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add() ' The Add() function takes one parameter. See my previous article about this.
Word stores the Bullet lists in what's called as "Galleries" - a couple of predefined ways to display lists in different ways. Therefore we need to retreive the galleries object from wich we will get the desired bullet list:
Dim objListGalleries As Variant ' Holder for the galleries
Set objListGalleries = objWord.Application.ListGalleries
Now, we will need a bunch of CONSTANTS:
Const wdUndefined = &H98967F
Const wdTrailingTab = 0
Const wdListNumberStyleBullet = &H17
Const wdListLevelAlignLeft = 0
Const wdListApplyToWholeList = 0
Const wdWord9ListBehavior = 1
Const wdNumberParagraph = 1
Const wdBulletGallery = 1
Const wdNumberGallery = 2
Const wdColorAutomatic = &HFF000000
Const wdColorDarkRed = &H80
Const wdColorDarkBlue = &H800000
Const wdLine = 5
Later on, we will need to convert the "Position" to Points, rather than centimeters (if You are using the metric system). I have a function called "CentimetersToPoints()":
Public Function CentimetersToPoints(Byval cm As Single) As Single
' 1 cm = 28.35 points
CentimetersToPoints = (cm * 28.35)
End Function
You could skip this function and set the positions with point-values directly, but working with centimeters or inches is far easier that points...
Now we can set the gallery with the look and feel of Your choise:
With objListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = Uchr$(61623)
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleBullet
.NumberPosition = CentimetersToPoints(0.63) ' See the function described earlier!
.Alignment = wdListLevelAlignLeft
.TextPosition = CentimetersToPoints(1.27)
.TabPosition = CentimetersToPoints(1.27)
.ResetOnHigher = 0
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = "Symbol"
End With
.LinkedStyle = ""
End With
Next step is now to give a name to the gallery, but since we are not saving the gallery and will only use it this one time, we will set the name to an empty string:
objListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
Now, we will apply the gallery as soon as we want to start to use the bullet list:
objWord.Selection.Range.ListFormat.ApplyListTemplate objListGalleries(wdBulletGallery).ListTemplates(1), False, wdListApplyToWholeList, wdWord9ListBehavior
Then type the text... In the example here we will have two bullets with the first entry stating ASDF and the second will be GHIJKLM.
objWord.Selection.TypeText "ASDF"
objWord.Selection.TypeParagraph
objWord.Selection.TypeText "GHIJKLM"
objWord.Selection.TypeParagraph
When the bullet list is complete, then turn off the listformat/gallery to resume normal text:
objWord.Selection.Range.ListFormat.RemoveNumbers wdNumberParagraph
objWord.Selection.TypeText "Another line"
Now, all we have to do is to save the document:
objDoc.SaveAs("C:\Temp\testdocument.docx")
objDoc.Close
objWord.Quit
' Garbage collection of variables:
Set objListGalleries = Nothing
Set objDoc = Nothing
Set objWord = Nothing
That's it! This is how to create a word document with bullet list - completely programmatically.