Bullet points from VBA / VBScript

D

dhnriverside

Hi peeps

Im using VBScript to create word doc, and I want to include bullet points in
part of the document. I've recorded a Macro, but I'm not sure how much of the
code I need.

What's the simplest code for creating a bullet list?!

Cheers


Dan
 
C

Chuck

To create a bulletted list in code the easiest way I can think of is assign a
style that uses bullets to the text you want to format as a bulletted list.
You can create that style manually in your template and then refer to it in
code (eg Selection.Range.Style = "YourBulletStyleNameHere").

As a (much) more advanced option, assuming you want to learn how to create
bullet (or any numbering style) in code (the following is very detailed but
hopefully it will save you and maybe others some hair pulling that I went
through...):

Unfortunately when you record a macro to generate code for paragraph
numbering, VBA uses ListGalleries which are completely unreliable (the index
number of a particular ListTemplate can change over time). IMHO the *only*
reliable way to access and use a particular ListTemplate (numbering, bullets,
whatever) is to define a ListTemplate and name it. (Take a look at the MVP
site for some background information which is unfortunately not entirely
complete, as well as the Google groups word programming archives for a
historical perspective on the headaches and heartaches Word's
buggy/inconsistent/undocumented list template feature have caused over the
years.)

Prior to rebuilding all our styles and list templates we experienced regular
paragraph numbering corruption. After rebuilding all list templates through
code with names, there haven't been any problems with numbering systems going
awry.

It's not clear from your post whether you want to create a simple one-level
bullet list or an outline list. Assuming for simplicity's sake you want a
simple one-level list...

First create your style (eg "Bullet1") and then create the list template you
want to associate it with.

CREATE THE STYLE

The following code checks whether Bullet1 style exists, creating it if it
doesn't, then redefines it. You only run this sub once in any document or
once on a template and then let the style definition pass along to any docs
created off that template. Once you've created the style using this code you
can then apply it to text (eg Selection.Range.Style =
"YourBulletStyleNameHere").

Obviously you need to adjust parameters like font etc to suit your needs
(I've remmed out the parameters to save space, see help or recorded macro
results for available parameters):

Sub StyleBullet1()

Dim styStyle As Style
Dim blnStyleExists As Boolean

blnStyleExists = False

For Each styStyle In ActiveDocument.Styles
If styStyle.NameLocal = "Bullet 1" Then
blnStyleExists = True
Exit For
End If
Next styStyle

If blnStyleExists = False Then
ActiveDocument.Styles.Add "Bullet 1"
Else
'do nothing
End If

With ActiveDocument.Styles("Bullet 1")
.AutomaticallyUpdate = False
.BaseStyle = "Normal"
.NextParagraphStyle = "Bullet 1"
End With
With ActiveDocument.Styles("Bullet 1").Font
' set font parameters here
End With
With ActiveDocument.Styles("Bullet 1").ParagraphFormat
'set paragraph format params here
End With
ActiveDocument.Styles("Bullet 1").ParagraphFormat.TabStops.ClearAll
With ActiveDocument.Styles("Bullet 1").ParagraphFormat
With .Shading
'set shading params here
End With
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
With .Borders
.DistanceFromTop = 1
.DistanceFromLeft = 4
.DistanceFromBottom = 1
.DistanceFromRight = 4
.Shadow = False
End With
End With
ActiveDocument.Styles("Bullet 1").LanguageID = wdEnglishIreland
ActiveDocument.Styles("Bullet 1").NoProofing = False
End Sub

CREATE A LIST TEMPLATE

Next you create a list template to associate the style to. As with the
style definition code above, you only run this sub once either on your
document or the template you're using to create documents. The code below
checks whether a specific named list template exists ("BulletLT") and
attaches to it if it does, creates it if it doesn't. The sample code below
only creates the first level but you can create up to 9. Note that the code
names the list template -- Set lstListTemplate =
ActiveDocument.ListTemplates.Add (OutlineNumbered:=True, Name:="BulletLT") --
and assigns a style to each level -- in this case .LinkedStyle = "Bullet 1".
Again, some parameters remmed out for space:

Sub BulletLevelsLT()

Dim lstListTemplate As ListTemplate
Dim blnLTExists As Boolean

On Error GoTo errorhandler

blnLTExists = False

For Each lstListTemplate In ActiveDocument.ListTemplates
If lstListTemplate.Name = "BulletLT" Then
blnLTExists = True
Exit For
End If
Next lstListTemplate

If blnLTExists = False Then
Set lstListTemplate = ActiveDocument.ListTemplates.Add _
(OutlineNumbered:=True, Name:="BulletLT")
ElseIf blnLTExists = True Then
Set lstListTemplate = ActiveDocument.ListTemplates("BulletLT")
End If

With lstListTemplate.ListLevels(1)
.NumberFormat = ChrW(61623)
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleBullet
.NumberPosition = CentimetersToPoints(0)
.Alignment = wdListLevelAlignLeft
.TextPosition = CentimetersToPoints(1.25)
.TabPosition = CentimetersToPoints(1.25)
.ResetOnHigher = 0
.StartAt = 1
With .Font
‘font params here – bullets are Symbol font
.Name = "Symbol"
End With
‘specify the style name you’re linking this bullet
‘list to:
.LinkedStyle = "Bullet 1"
End With

‘If you want to add levels, copy the above code
‘for level 1 and change the level numbers and
‘parameters accordingly

‘Link the style to the list template here
‘you only need to do this for level 1
ActiveDocument.Styles("Bullet 1").LinkToListTemplate _
ListTemplate:=lstListTemplate, ListLevelNumber:=1

Exit Sub

errorhandler:
MsgBox Err.Description
Exit Sub

End Sub

This may be way more than you wanted but then again, but I hope it helps...

Chuck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top