Code causes Word to Stop Responding

T

tighe

All,

i have code run a document but it freezes if the document is too large. the
document this code runs on can be anywhere from 200-500 pages. it ssems to
work fine on anything under 300.

i am running this in word 2007 being called from Access 2007.

any advice on making the code better or keep Word from stop responding would
be terrific.

thanks in advance.

Sub bulletFormat()
'
' bulletFormat Macro
'
Dim doc As Word.Document
Dim para As Word.Paragraph
Set doc = ActiveDocument
For Each para In doc.Content.ListParagraphs
para.Range.ListFormat.ApplyListTemplateWithLevel
ListTemplate:= _
ListGalleries(wdBulletGallery).ListTemplates(1),
ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToSelection, DefaultListBehavior:= _
wdWord10ListBehavior
Next
End Sub
 
D

DaveLett

Hi,
Do you use a paragraph style for your bulleted lists? If so, you could use a
search and replace, which would be MUCH faster than cycling through the
paragraphs (very slow anyway) of your lists.

Dave
 
T

tighe

Dave,

thanks for the response, that was an ealrier method i came to before this
code. i found that when paragraph style is applied is does not differentiate
between list levels and everything becomes list level one, so the code was
removing items that were sub-bullets. that is why i am currently trying this
code, I no longer have the code i was using to do that. so i don't have an
example to see if there were errors in how i was coding that action.
 
P

Pesach Shelnitz

Hi,

Although your macro does what you want, it also does something else that I
believe is detrimental and may be causing your problem in large files. In
particular, your macro makes each item in every list a separate list (List
object). You can see this happening by creating (not copying from another
doc) two or three short lists in a new document and running the following
macro.

Sub ShowLists()
Dim par As Paragraph
Dim i As Long
Dim j As Long

With ActiveDocument
For j = 1 To .Lists.Count
MsgBox "List " & j & " contains " & _
.Lists(j).ListParagraphs.Count & " items."
For i = 1 To .Lists(j).ListParagraphs.Count
MsgBox .Lists(j).ListParagraphs(i).Range.Text
Next
Next
End With
End Sub

The macro should show you what each list contains. Now run your macro and
then run this macro again. At this point you should see that each list has
been broken up into separate lists for each item.

As Dave suggested to you, the better way to format lists is to apply styles
and modify those styles. You would need a separate style for each bulleting
level. To get a better understanding of how numbering can go wrong in Word,
see Word's Numbering Explained
(http://www.word.mvps.org/FAQs/Numbering/WordsNumberingExplained.htm) by John
McGhie. Follow the links to the Simple List Numbering and Outline List
Numbering pages, which explain the main issues that you need to take into
account when you write any code to format bullets or numbering.
 
T

tighe

Pesach,

thank you for the response. i did realize that the code is going to each
list item to affect the proper bullet style and keep the proper level, and
that is why this was the last iteration of trying to get this accomplished.

as for the word.mvps site i am not sure how to use this information i norder
to build better code. the reason i have finally come to this code is that it
keeps necessary formatting but obviously is no good if word freezes while
using it.

i went back and tried to find the previous code because maybe it could have
been tweaked to keep the level needed but cannot. i do recall my the code
applied paragraph style to the list as a whole therefore removing any levels
associated, and code not figure out how to write something tot he effect of
"find level2, applye stylelist2" or something of that nature. once again
suggestions would be great. i am not married to the current method, just
posting what i have now.

To give a little background on this:
the information is help in a memo field in a Access 2007 DB. i export to
Html, which keeps the proper bullet format. then i edit the Html to remove
the top row, tite of the table exported, and save to docx. i then use the
docx as a merge source for a document.
the funny thing is after the merge only the first few lists show the list
character picture, but all the other format is maintained from the merge
source and if you vlick on a list is show in the ribbon thats its format is
list. when that document is closed and opened then you can actually see the
list character picture but there are now numbered instead of the bullet
format, even though the merge source and exported items had the proper bullet
format.

maybe there is something that can be done in order to keep the proper format
before hand. the merge field already has the MERGEFIELD taken out which i
supposed to maintain format from the merge source but again i have found this
is not the case since in the DB you must Italize and bold something to have
it merge bold.

I realize this is a com,plicated but is is the way i have found that the
final document returns to as close to what formatting is needed without
interferring with content entered by the user in the DB expect these bullets.
 
T

tighe

Pesach,

i was not focusing on this for a while but finally with the list count code
of yours and few other resources, i found a change that works, the code runs
in about a minute for a 800 page document with many lists and unlike the
previous code sub-bullets have a different picture associated with them, it
still keeps sub bullets. you will notice that the only difference in the
code is that i look for lists instead of paragraphs.

Sub bulletFormat()
Dim doc As Word.Document
Set doc = ActiveDocument
Dim LT As List
For Each LT In doc.Lists
LT.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdBulletGallery).ListTemplates(1),
ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToSelection, DefaultListBehavior:= _
wdWord10ListBehavior
Next
End Sub
 

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