Bulleted lists with Word VBS

R

Richard John

Does anyone know how to create a bulleted list using Word 2000 VBA (XP Pro
SP2)? I have tried, but the VBA I've written seems to want to only bullet
every alternate item. The broad format of the document I want is:

- 423 Clients were surveyed
- 251 clients or 59% did not return the survey
- 172 clients or 41% completed the survey

Subheading 1
- 75% considered that a worker was allocated in a reasonable time.
- 90% felt that the plan looked at what they could do

Subheading 2
- Faster response time following request.
- worker is to be highly commended.

What I am getting is every alternate point being bulleted. Like:
=================================================
423 Clients were surveyed
- 251 clients or 59% did not return the survey
172 clients or 41% completed the survey

- Subheading 1 (WRONG)
75% considered that a worker was allocated in a reasonable time.

Here is code the fragment that does this:
===========================

If sClass = "heading" Then 'If it is a heading, then bold
it and insert a paragraph feed
With Selection
.TypeParagraph
.Font.Bold = True
.TypeText Text:=sData
.Font.Bold = False
.TypeParagraph
End With
else 'Otherwise add a bullet
With Selection
.Range.ListFormat.ApplyBulletDefault
.TypeText Text:=sStatement
.TypeParagraph
End With
End If


What's wrong with the code?



Second question: How can I programmatically delete a page I don't want, from
a Word document? Can't find anything that will help me.

Thanks
 
C

Cindy M -WordMVP-

Hi Richard,
Does anyone know how to create a bulleted list using Word 2000 VBA (XP Pro
SP2)? I have tried, but the VBA I've written seems to want to only bullet
every alternate item. The broad format of the document I want is:
I'd approach this differently. I'd create a STYLE that contains the formatting
I require (including the bullet). I'd then apply the style to the RANGE objects
that should be bulleted.

The problem with your code is that you're mimicking the user's actions; relying
on the Selection object (used the macro recorder?). For very simple things, one
can work this way, but it's not "best practise"; the developer should work with
the object model whenever possible. I'd work more like this:
Dim doc as Word.Document
Dim rng as Word.Range

Set doc = Documents.Add
Set rng = doc.Content
rng.Collapse
'You're doing some data gather here, in a loop, I presume
If sClass = "heading" Then
rng.Text = vbCR & sData & vbCR
rng.Style = "StyleName"
rng.Collapse wdCollapseEnd
Else
rng.Text = sStatement & vbCR
rng.Style = "OtherStyleName"
rng.Collapse wdCollapseEnd
End If

If you want to see what this does, stick a rng.Select in the code every time I
use rng.Collapse so that you can see where the range is. (This is a good way to
troubleshoot, as Word doesn't always put a range where you might expect...)

Deleting a page: Yep, this one is something only a real "Wordie" would know,
and harks back over a decade to the old WordBasic days. MS didn't give us any
"page" object to manipulate; but originally they did give us a set of built-in
bookmarks we can use to pick up all the text between two style headings, or a
page. Since this is old stuff, it does rely on the SELECTION - it's one of the
exceptions to the rule about using ranges. Move the selection to the page you
want to delete, then:
doc.Bookmarks("\Page").Range.Delete
- 423 Clients were surveyed
- 251 clients or 59% did not return the survey
- 172 clients or 41% completed the survey

Subheading 1
- 75% considered that a worker was allocated in a reasonable time.
- 90% felt that the plan looked at what they could do

Subheading 2
- Faster response time following request.
- worker is to be highly commended.

What I am getting is every alternate point being bulleted. Like:
=================================================
423 Clients were surveyed
- 251 clients or 59% did not return the survey
172 clients or 41% completed the survey

- Subheading 1 (WRONG)
75% considered that a worker was allocated in a reasonable time.

Here is code the fragment that does this:
===========================

If sClass = "heading" Then 'If it is a heading, then bold
it and insert a paragraph feed
With Selection
.TypeParagraph
.Font.Bold = True
.TypeText Text:=sData
.Font.Bold = False
.TypeParagraph
End With
else 'Otherwise add a bullet
With Selection
.Range.ListFormat.ApplyBulletDefault
.TypeText Text:=sStatement
.TypeParagraph
End With
End If


What's wrong with the code?



Second question: How can I programmatically delete a page I don't want, from
a Word document? Can't find anything that will help me.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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