Error formatting Word Lists on automation

P

Patrick

I've got a macro written that should be finding all of the
lists of a given style and changing the margin for them.
I've tried doing it about 3 different ways, and get some
interesting problems.

If I try it like this:
---snippet---
Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute
Selection.Find.ClearFormatting

With ListGalleries
(wdOutlineNumberGallery).ListTemplates(2).ListLevels(3)
.NumberFormat = "%2."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(0.75)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(1)
.TabPosition = InchesToPoints(1)
.ResetOnHigher = 2
.StartAt = 1
.LinkedStyle = "List Ordered-Lower Case"
End With

Selection.Range.ListFormat.ApplyListTemplate
ListTemplate:=ListGalleries( _
wdOutlineNumberGallery).ListTemplates(2),
ContinuePreviousList:=False, _
ApplyTo:=wdListApplyToWholeList
--- end snippet ---
it changes the for the lists surrounding the lists I need
to reformat.

If I try it like:
--- snippet ---
On Error Resume Next

Dim snt As Word.Range
Dim retval As String

On Error Resume Next
Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute
Selection.Find.ClearFormatting
With ActiveDocument.Content.Find
.ClearFormatting
.Style = "List Ordered-Lower Case"
Do While .Execute(findText:="", Forward:=True,
Format:=True) = True _
And Selection.Range.End <>
ActiveDocument.Content.End
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.Paragraphs(1).Range.Select
.Move Unit:=wdParagraph, Count:=1
End With
For Each snt In Selection.Sentences
If snt.Style = "List Ordered-Lower Case"
Then
Selection.Range.ListFormat.ListIndent
End If
Next snt
Loop
End With

--- end snippet ---
it works except that it won't indent the first line of my
lists.


So can someone tell me what's wrong with my code? I've
been banging my head on this wall for about a day and a
half now.
 
B

Bruce Brown

Patrick - A day and a half of head-banging due to list templates, is
that all? Consider your head lucky.

The following assumes that your "List Ordered-Lower Case" style is
validly linked to an outline-numbered list template at level 3 and
that the first two levels are also validly linked to two other
outline-numbered styles. In other words, the list template must have
been created and styles properly linked at levels 1-3 *before* running
the macro.

In line #3, replace "Heading 1" with the name of your level 1 linked
style.

Dim L As ListTemplate
For Each L In ActiveDocument.ListTemplates
If L.ListLevels(1).LinkedStyle = "Heading 1" Then
With L.ListLevels(3)
.NumberPosition = InchesToPoints(0.75)
.TextPosition = InchesToPoints(1)
.TabPosition = InchesToPoints(1)
.LinkedStyle = "List Ordered-Lower Case"
End With
Exit For
End If
Next

If only you could see the heads of others who've played around with
outline numbering and list templates. We're no longer fit to go out
in public.

- Bruce
 
J

JGM

Hi Bruce,

I concur! I had to do a template that would allow a client to copy-paste
many documents (luckily all based on templates I created) that had 6
different numbering schemes and tons of "Reset to 1" in all of the
documents.... And the numbering could not change after the copy-paste in the
main document.... I could not go out for weeks!

Looking at your code, a thought came up... To make it easier, couldn't
Patrick had named his ListTemplate when he linked the styles with the levels
in the Bullet/Numbers dialog and call it in his macro?

Something like:

Dim L As ListTemplate
Set L = ActiveDocument.ListTemplates("MyNamedList")
With L.ListLevels(3)
.NumberPosition = InchesToPoints(0.75)
.TextPosition = InchesToPoints(1)
.TabPosition = InchesToPoints(1)
.LinkedStyle = "List Ordered-Lower Case"
End With
etc...

Would that work?

Just curious...
TIA
 
B

Bruce Brown

Jean-Guy,

You raise a key point about working with list templates in VBA.

Had Patrick named the list template, then yes . . .

Set L = ActiveDocument.ListTemplates("MyNamedList")

.. . . would be easier by virtue of its being shorter. However, what if
you don't know whether a list template has been named or not? How do
you then pinpoint the exact list template you need out of, say, a
collection of 95 -- some bulleted, some outline-numbered, some
single-level lists?

The foolproof way to access the correct list template -- it will never
fail you -- is to test the list template's level 1 linked style
against what you know to be your level 1 linked style:

For Each L In ActiveDocument.ListTemplates
If L.ListLevels(1).LinkedStyle = "Heading 1" Then . . .

Nota bene: it must always be ListLevels(1). Any higher ListLevel
number will give you an error message.

You can name a list template by first killing off any existing list
template name that happens to be "MyNamedList," then naming the list
template you want immediately afterwards:

For Each L In ActiveDocument.ListTemplates
If L.Name = "MyNamedList" Then
L.Name = ""
Exit For
End If
Next
For Each L in ActiveDocument.ListTemplates
If L.ListLevels(1).LinkedStyle = "Heading 1" Then
L.Name = "MyNamedList"
Exit For
End If
Next

.. . . then proceed by setting L to "MyNamedList."

Another key point about list templates is that the last line of your
With ListLevels(x) code must *always* be .LinkedStyle =
"MyNumberedStyle x." If
you were to overlook this linkage as follows . . .

With L.ListLevels(3)
.NumberPosition = InchesToPoints(0.75)
.TextPosition = InchesToPoints(1)
.TabPosition = InchesToPoints(1)
End With

.. . . you'd see that the paragraph settings for "MyNumberedStyle x"
have been changed in the style's paragraph dialog box, but they do not
appear on the screen! Head-bashing time again.

A final key point about working with list templates in VBA is that the
quickest ticket to insanity is to work with ListGalleries, as Patrick
was doing:

With ListGalleries
(wdOutlineNumberGallery).ListTemplates(2).ListLevels(3) . . .

It's only natural at first that you should try to record a macro that
accesses the list template through the ListGalleries. But
ListGalleries in VBA = death.

* * *

Now that you can go out in public again, Jean-Guy, you might want to
catch the 1959 horror-film classic "Yeux Sans Visage," dirige par
Georges Franzu. It's making the rounds of art-film houses in the U.S.
now; maybe it's on its way north or already there.

This black-and-white shocker is about a surgeon whose daughter's face
was disfigured in a car accident. The surgeon's mistress kidnaps
young girls to a gothic castle outside Paris, where the surgeon
removes their faces in a basement operating room remeniscent of Dr.
Frankenstein's and grafts them onto his daughter's face --
unsuccessfully, of course.

Guaranteed to give you permanent memories of the list template kind.

- Bruce
 
W

Word Heretic

G'day (e-mail address removed) (Bruce Brown),

Re the 'never fail you' - what about fractured list templates where
multiple LT's are in use for one style throughout the document? You
have several LTs with that style listed as the first level...


(e-mail address removed) (Bruce Brown) was spinning this yarn:
Jean-Guy,

You raise a key point about working with list templates in VBA.

Had Patrick named the list template, then yes . . .

Set L = ActiveDocument.ListTemplates("MyNamedList")

. . . would be easier by virtue of its being shorter. However, what if
you don't know whether a list template has been named or not? How do
you then pinpoint the exact list template you need out of, say, a
collection of 95 -- some bulleted, some outline-numbered, some
single-level lists?

The foolproof way to access the correct list template -- it will never
fail you -- is to test the list template's level 1 linked style
against what you know to be your level 1 linked style:

For Each L In ActiveDocument.ListTemplates
If L.ListLevels(1).LinkedStyle = "Heading 1" Then . . .

Nota bene: it must always be ListLevels(1). Any higher ListLevel
number will give you an error message.

You can name a list template by first killing off any existing list
template name that happens to be "MyNamedList," then naming the list
template you want immediately afterwards:

For Each L In ActiveDocument.ListTemplates
If L.Name = "MyNamedList" Then
L.Name = ""
Exit For
End If
Next
For Each L in ActiveDocument.ListTemplates
If L.ListLevels(1).LinkedStyle = "Heading 1" Then
L.Name = "MyNamedList"
Exit For
End If
Next

. . . then proceed by setting L to "MyNamedList."

Another key point about list templates is that the last line of your
With ListLevels(x) code must *always* be .LinkedStyle =
"MyNumberedStyle x." If
you were to overlook this linkage as follows . . .

With L.ListLevels(3)
.NumberPosition = InchesToPoints(0.75)
.TextPosition = InchesToPoints(1)
.TabPosition = InchesToPoints(1)
End With

. . . you'd see that the paragraph settings for "MyNumberedStyle x"
have been changed in the style's paragraph dialog box, but they do not
appear on the screen! Head-bashing time again.

A final key point about working with list templates in VBA is that the
quickest ticket to insanity is to work with ListGalleries, as Patrick
was doing:

With ListGalleries
(wdOutlineNumberGallery).ListTemplates(2).ListLevels(3) . . .

It's only natural at first that you should try to record a macro that
accesses the list template through the ListGalleries. But
ListGalleries in VBA = death.

* * *

Now that you can go out in public again, Jean-Guy, you might want to
catch the 1959 horror-film classic "Yeux Sans Visage," dirige par
Georges Franzu. It's making the rounds of art-film houses in the U.S.
now; maybe it's on its way north or already there.

This black-and-white shocker is about a surgeon whose daughter's face
was disfigured in a car accident. The surgeon's mistress kidnaps
young girls to a gothic castle outside Paris, where the surgeon
removes their faces in a basement operating room remeniscent of Dr.
Frankenstein's and grafts them onto his daughter's face --
unsuccessfully, of course.

Guaranteed to give you permanent memories of the list template kind.

- Bruce

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

Replies offlist may require payment.
 
B

Bruce Brown

Good point, Steve.

To be quite honest with you, I am not familiar with the "fractured
list templates" you describe, where "multiple LTs are in use for one
style throughout the document," so I set out to create a few in Word
2002.

I used three methods in attempting to reproduce your fractured LTs:

* Editing the LTs through Format > Bullets and Numbering
* Editing the LTs through the outline-numbered styles
* Using VBA, both with ListLevels(1).LinkedStyle and, for styles,
LinkToListTemplate.

Nothing worked. No matter how hard I tried, I could not link one
style to more than one list template; I couldn't even link one style
to more than one level in the *same* list template.

(Occasionally, using Format > Bullets and Numbering, a Customized
Outline Numbered List dialog would indicate that dual linkage had been
achieved, but each time it proved to be a mirage.)

Typical experiment: Two list templates, one linked to all 9 built-in
Heading styles, the other linked to 3 levels of user-defined
outline-numbered styles. I linked Heading 3 to the third level of the
three-level LT. Result: Heading 3 became part of that LT, the style
which had previously been linked to level 3 was booted out of the LT
and assumed the properties of the Normal style, and the third level of
the nine-level list template was linked to (no style).

The numbering style for level 3 of both LTs, however, remained the
same.

Summary of findings, superficial as they may be: one slot per level
per list template to any one style. (Styles *based on* already linked
outline-numbered styles, that can parasitically share the same list
template for a while, are not being referred to here.)

* * *

Unwilling to accept the evidence of my own eyes, I searched through
the numbering threads for posts involving "different list templates."
A number of threads touched on assigning different sets of styles to
the *same* list template, but none that I could find specifically
grappled with the question of linking the same style to *different*
list templates.

At least twice, however, the latter was mentioned in passing without a
clue as to how users could inadvertently create such a beast. The
beast was assumed to exist, period. It was reminiscent of the
medieval unicorn portrayed in writing, painting and tapestry --
oft-described, rarely seen -- like the budget surpluses proclaimed by
modern-day federal governments.

* * *

Steve, the Unicorn of One Style Linked to Multiple List Templates may
well exist. It may well be that this horned thing you speak of grazes
in herds in versions earlier than Word 2002. It may well be that I
overlooked some obvious way to link one style to multiple LTs.

But I decline to spend another unpaid second trying to create
"fractured list templates"; it's hard enough to create good ones and
get paid for it.

Instead, I beseech you to show us exactly how to create the beast you
describe, step-by-step, in Word 2002. After all, a "how-*not*-to" can
be every bit as valuable as a "how-to." - Bruce
 
M

Margaret Aldis

Hi Bruce and Steve

I think maybe I can explain the different things you see.

If you use code like this:

Dim LT As ListTemplate
For Each LT In ListGalleries(wdOutlineNumberGallery).ListTemplates
MsgBox LT.ListLevels(1).LinkedStyle
Next LT

When you have been setting up many different style-list template linking
using the Bullets & Numbering dialog, you will get answers that reflect the
appearance of the dialog panes - that is, it will appear that several
different list templates are linked to the same style.

If, however, you test the list templates in the *document*

Dim LT As ListTemplate
For Each LT In ActiveDocument.ListTemplates
MsgBox LT.ListLevels(1).LinkedStyle
Next LT

even though you may have applied several list templates linked to the same
styles during the history of the document (and we know they cannot be
deleted ...) you will only ever find one list level linked to one style.

The other list templates in the gallery are not actively affecting the
style, but of course should you click on the 'wrong pane' you will change
the linking and cross-wire the style to another list template - a common
user error.

I think this is just another example of why the ListGalleries collection is
not a good place to be.

--
Margaret Aldis - Microsoft Word MVP
Syntagma partnership site: http://www.syntagma.co.uk
Word MVP FAQ site: http://www.mvps.org/word
 
J

JGM

Hi Maragaret,

Thanks for shedding light on a neboulous topic that Word has never
succesfully managed to explain in its help files or even implement... When
so many users are having nightmares with this, I think we have to point the
finger toward the software...

It is about time that they rethink their whole approach... Has this been
addressed in the Office 2003 version?

Thanks again.

Cheers!
 
B

Bruce Brown

Margaret, Steve and Jean-Guy

Thank you for that simple clarification, Margaret. Having said in an
earlier post that Outline Numbered List Galleries are death to work
with, I thought Steve was referring to list templates in the document.

Since "you will only ever find one list level linked to one style," as
Margaret says, I reiterate the point that testing the list template's
level 1 linked style against what you know it to be is an ideal way to
identify the list template, whether it's named or not.

- Bruce
 

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