ListTemplate issues

J

JGM

Hi there,

I have the following code (part of a larger "Style Resetting" macro):
'_______________________________________
Dim Mydoc As Document
Set Mydoc = ActiveDocument
'THEN FIND OR CREATE THE LIST TEMPLATE
For Each oLstTemplate In Mydoc.ListTemplates
If oLstTemplate.Name = "Alphaabc" Then
TemplateFound = True
Exit For
End If
Next oLstTemplate
'----------------------------------------------------------------
If TemplateFound Then
Set oLstTemplate = Mydoc.ListTemplates("Alphaabc")
Else
Set oLstTemplate = Mydoc.ListTemplates.Add(OutlineNumbered:=True,
Name:="Alphaabc")
oLstTemplate.Name = "Alphaabc"
End If
'_______________________________________

It used to work fine, but now, in a particular document, it always returns
"False" for "TemplateFound," so it goes to the "Set" line under the "Else"
statement, and when it gets to
oLstTemplate.Name = "Alphaabc"
an error is generated becasue Word tells me that the name already exists.
In the "For" loop I have checked all the names that are generated. I have 39
list templates, only one has a name, all others return a "" string for the
name.
How can it not find the name, and then refuse to create it because the name
already exists?
If it cannot find it, how does it know that it already exists?

Confused!
TIA
 
R

Rich Conway

Unfortunately, this is caused by a bug in Word. There is
currently no workaround, other than to copy/paste the
entire document, excluding the last paragraph mark, info a
new document. The issue can be reproduced, using the
instructions below, without using VBA at all.

Rich Conway
Kraft Kennedy & Lesser, Inc.
www.kkl.com


* Close Word
* Delete Normal.dot
* Start Word
* Turn off "Keep Track of Formatting" in Tools > Options >
Edit
* Reset the list galleries in the Outline Numbered tab of
Format > Bullets and Numbering
* Create a new document (Source Doc)
* Format > Styles and Formatting...
* Right-click Heading 1 and choose Modify
* Format > Numbering...
* Choose Outline Numbered tab
* Highlight one of the four bottom galleries
* Click Customize
* Click More
* Type "NAME1" into the "ListNum field list name" textbox
* Click OK to exit the "Customize Outline Numbered List"
dialog
* Click OK to exit the "Modify Style" dialog
* Add a Heading 1 paragraph into the document
* Add a Normal style paragraph into the document
* Select the Heading 1 paragraph, and Copy to the clipboard

* Create a new document (Destination Doc)
* Format > Styles and Formatting...
* Right-click Heading 1 and choose Modify
* Format > Numbering...
* Choose Outline Numbered tab
* Highlight one of the four bottom galleries (a different
one than last time)
* Click OK to exit the "Bullets and Numbering" dialog
* Click OK to exit the "Modify Style" dialog
* Paste the copied paragraph into this document
* Notice that the new paragraph does NOT have any
numbering!

* In the destination document, Format > Styles and
Formatting...
* Right-click Heading 1 and choose Modify
* Format > Numbering...
* Accept the highlighted gallery on the Outline Numbered
tab
* Click Customize
* Click More
* Type "NAME1" into the "ListNum field list name" textbox
* Click OK to exit the "Customize Outline Numbered List"
dialog - error message appears
 
B

Bruce Brown

Jean-Guy

You might want to go back to the thread "Error formatting Word Lists
on automation." See the 4th post, Nov 15, in reply to your post of
Nov 14.

No money-back guarantees. Just wanted to remind you we've been there
before.

- Bruce
 
J

JGM

Hi Bruce,

Thanks for the reminder..

But this is different: (by the way, no where in my code to I even come close
to using/referencing/looking/peeking in the dreaded ListGallery object...)
Here, I have a list template that I have created through VBA, along with all
its levels, doing exactly as you recommended (Like using ".LinkedStyle =" as
the last line of each "With" section...). Then, when I rerun the code, I
expect to find the list template (remember that I have created it before).
The problem I have is that the code tells me that my list template does not
exist, and then, when I try to create it, it says it cannot because the name
already exists. How can it not find the name, and then refuse to create one
because the name already exists? What is the difference between

For Each oLstTemplate In Mydoc.ListTemplates
If oLstTemplate.Name = "Alphaabc" Then
TemplateFound = True
Exit For
End If
Next oLstTemplate

Wich returns False, i.e. the list name does not exist,
and:

Set oLstTemplate = Mydoc.ListTemplates.Add (OutlineNumbered:=True)
oLstTemplate.Name = "Alphaabc"

which returns an error becasue the name already exists.
If it does, how come the previous few lines of code could not find it?

But I have found a "logic" to my problem:

I ran the code against a blank document created from the template in which
the list templates where created with the code.
It ran flawlessly. As expected, it found the list template as already
existing, and instead of creating it, I was able to use a "Set" statement to
grab it.

The error seems to be generated only when the code runs in a document that
contains text that has been pasted in from documents based on templates in
which the exact same code had been executed to create the list templates in
all those templates (Call them "Editing Templates"). So, the exact same code
was run in various templates ("Editing Templates") to make sure that the
they all had exactly the same list templates and style definitions. If I use
a destination document (also based on a template (Call it "Print Template")
in which the code had been executed) in which to paste text from documents
created based on the "Editing Templates", the styles are maintaned and
everything works as planned. The destination document is used for printing,
not editing. But when I re-run the code in that destination document (based
on the "Print Template"), I get the error I describe above. So, I guess
having references to the same named list template, but originally from
different documents (even though the last paragraph mark was not included in
any of the paste) screws up the destiantion document's ListTemplate
reference table...

Does that make any sense?

Cheers and thanks again.

p.s. I haven't had the chance to see (or even hear about) the "Yeux sans
visage" movie.... still hoping!
 
B

Bruce Brown

J-G

Could not duplicate your problem after creating three different
templates, each with list templates linked to the built-in Heading
styles and each with the same name. Created docs from each template
and pasted a few paragraphs (all styled in the Heading styles) from/to
each such document, then used your code without triggering an error.

Am curious if you tried killing off the list template name first . . .

For Each L in ActiveDocument.ListTemplates
If L.Name = "Alphaabc" Then L.Name = ""
Exit For
Next

'then pinpointing the list template where list levels 1 is linked to
Heading 1

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

're-linking each style to the list template

For k = 1 to 9
L.ListLevels(k).LinkedStyle = "Heading " & k
Next

're-naming the list template using the same name

L.Name = "Alphaabc"

'doing the rest of your stuff with styles here

'then re-re-linking each style to the list template, yet again!

For k = 1 to 9
L.ListLevels(k).LinkedStyle = "Heading " & k
Next
Exit For
End If
Next

My experience is that the "List template already exists" problem only
occurs when one of the styles has become unlinked from a named list
template.

It may well be that the steps Rick Coleman posted are absolutely
necessary. Was hoping you could get away with VBA instead.

What a maddening problem. - Bruce P.S. You are using the built-in
Heading styles, right? And you are in Word 2002, right?
 
J

JGM

Bruce Brown said:
J-G

Could not duplicate your problem after creating three different
templates, each with list templates linked to the built-in Heading
styles and each with the same name. Created docs from each template
and pasted a few paragraphs (all styled in the Heading styles) from/to
each such document, then used your code without triggering an error.

Am curious if you tried killing off the list template name first . . .

For Each L in ActiveDocument.ListTemplates
If L.Name = "Alphaabc" Then L.Name = ""
Exit For
Next

Tried, but no can do... Beause it does not find the name in the "For" loop.
It only finds it when I try to assign the name, which the code does when the
"For" loop is negative, i.e., the name was not found... This is a typical
Catch-22 case (Yet another great movie based on an even greater book!)
'then pinpointing the list template where list levels 1 is linked to
Heading 1

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

're-linking each style to the list template

For k = 1 to 9
L.ListLevels(k).LinkedStyle = "Heading " & k
Next

're-naming the list template using the same name

L.Name = "Alphaabc"

'doing the rest of your stuff with styles here

'then re-re-linking each style to the list template, yet again!

For k = 1 to 9
L.ListLevels(k).LinkedStyle = "Heading " & k
Next
Exit For
End If
Next

My experience is that the "List template already exists" problem only
occurs when one of the styles has become unlinked from a named list
template.

It may well be that the steps Rick Coleman posted are absolutely
necessary. Was hoping you could get away with VBA instead.

You mean Rich Conway?
If so, I followed his instructions, and when I got to the penultimate step,
I could not type NAME1 because it was already there. But I see what he
meant... There was a problem because the name should not have been there and
it did not matter wich list gallery scheme I chose, thay all presented me
with the NAME1 list template name already there...
What a maddening problem. - Bruce P.S. You are using the built-in
Heading styles, right? And you are in Word 2002, right?

No, I built all the styles from scratch (The code creates a base style
containing all the main attributes such as Font, indent, and so on (based on
"Nothing", not on "Normal"), then all the Outline styles are based on that
"basic" style) That may be the problem, Word hates it when we try to receate
its Heading 1 to 9 system. But I had to do it that way because in the same
"master"document (built from 50 to 150 independant documents) I jave 4
different outline numbering schemes (with letters, numbers, bullets...) and
I did not want to base all that on Heading 1... So I created 4 independant
systems, each with its own "basic" style based on nothing, so that each
numbering scheme's styles are based on their own basic style. After I create
the independant series of styles, I create the ListTemplate names and then I
associate the styles to the list template, following a procedure identical
to the one you outlined in the November 14 posts...
Since I built everything from scratch and that styles are flaky in Word,
especially numbered styles, even more so the outlined ones, and even more
more so the list templates... It is hardly surprising that I run into
problems!

Yes, Word 2002.

Thanks for your time.
 
B

Bruce Brown

Jean-Guy

Apologies to Mr. Conway for getting his last name wrong.

When styles have been linked to a template and you go to the Outline
Numbered Gallery clicking windows other than the framed one, you will
see NAME1 or whatever name you're using in *all* the windows. This is
a confusing fluke of the Gallery, and does not mean that all the other
list templates are named NAME1. So perhaps that caused confusion at
the pentultimate step and you stopped one step short of victory?

If you haven't done so already, I think you'd find it helpful to go to
the numbering thread and search for "named list templates." In those
posts you'll find a goldmine of historical experience and commentary
contributed by many a knowledgeable expert.

It might be advantageous to post your problem there too. - 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