Word Macro works with F8 - not at full speed

B

Brandon

I have been working on this problem for some time now and I just can't
figure it out. I wrote this macro to handle the Table export from
Telelogic's DOORS and create a document from it. It steps through the
table (Table 2 in template), finds certain things, counts the Table and
Figure captions and then converts it to text. It then steps through and
creates caption numbers, centers tables and figures, etc. When you step
through the macro manually (F8) everything works fine. When you run it
at full speed it skips certain Table centering steps and refuses to
update the numPages field. Here is one of the portions that isn't
working:

For TableNum = 1 To TableCount
If Selection.Find.Execute(FindText:="Table ?-?:",
MatchCase:=True, MatchWildcards:=True, Forward:=True) Then
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Style = ActiveDocument.Styles("Caption")
With CaptionLabels("Table")
.NumberStyle = wdCaptionNumberStyleArabic
.IncludeChapterNumber = True
.ChapterStyleLevel = 1
.Separator = wdSeparatorHyphen
End With
Selection.HomeKey Unit:=wdLine
Selection.InsertCaption Label:="Table",
TitleAutoText:="InsertCaption1", _
Title:="", Position:=wdLine, ExcludeLabel:=0
Selection.TypeText (": ")
With Selection.ParagraphFormat
.KeepWithNext = True
.OutlineLevel = wdOutlineLevelBodyText
End With
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.ParagraphFormat.Alignment =
wdAlignParagraphCenter
Selection.MoveUp Unit:=wdLine, Count:=1
End If
Next TableNum

As you can see, it finds the "Table ?-?:" text, deletes it, and inserts
a numbered caption. Then it steps down one line, centers the Table
below, and steps back up before searching for the next one. Seems
simple to me and it works fine most of the time, but it invariably does
not center 2 or 3 tables in the doc at full speed. Again, works fine
with F8.

The other issue is update numPages. I target the field in a known cell
in Table 1, then use "Selection.Fields.Update" to update it.
Stepping through works fine AND placing the same code at the beginning
of the macro works too; however, this doesn't capture the TOC addition
and formatting changes.

I have tried adding pauses to the code thinking it was just running too
quick, but that doesn't really make sense, nor does it work.

Anybody seen this problem before?
 
S

Shauna Kelly

Hi Brandon

At one time or another, I think we've all experienced the problem of code
that works fine when you F8 through it, but does not run properly.

My basic thinking is that, if Word can't cope with running all my code, then
I try to ensure that there is as little code as possible for it to run!

In your case this code:
With CaptionLabels("Table")
.NumberStyle = wdCaptionNumberStyleArabic
.IncludeChapterNumber = True
.ChapterStyleLevel = 1
.Separator = wdSeparatorHyphen
End With

is being run once for each table (because it's in the For TableNum = 1 to
TableCount loop). There's no need to do that, because the CaptionLabels
apply to the whole document, not just one table. So you can get that part of
the code out of the loop.

And this code:
With Selection.ParagraphFormat
.KeepWithNext = True
.OutlineLevel = wdOutlineLevelBodyText
End With

is being applied to a paragraph that to which you've applied the Caption
style. If you modified the Caption style with these two settings, you won't
need these lines of code at all.


The next thing to do is to avoid using the Selection object. When you use
the Selection, Word actually moves the selection around the document. It's
better to set a range at the beginning of your code and then manipulate the
range. For some information about that, see Jay Freedman's article on How to
modify a recorded macro at
http://word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm

Finally, it's not clear what this is doing
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Depending on the nature of the table, the .MoveDown will move to a single,
almost random cell in the first row of the table, and centre the first
paragraph in that cell.

If you want every paragraph in every table in the document to be centred,
then you can use something like:

Dim oTable as Word.Table

For Each oTable In ActiveDocument.Tables
oTable.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next oTable

If you only want paragraphs in the first row centred, you could use:

For Each oTable In ActiveDocument.Tables
oTable.Rows(1).Range.ParagraphFormat.Alignment =
wdAlignParagraphCenter
Next oTable

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
B

Brandon

Shauna,

Thanks so much for responding to my question. The reasoning behind
stepping through for each table is that there is Text in place already
of the right format, it just doesn't have the caption style attached to
it. Hence the For/Next search loop. This finds the tables and inserts
the Word Caption numbers and styles. I hadn't thought of attaching the
..keep with next and .outlinelevel to the caption style, I'll do that
right away.

I will look into ways around using the selection object and I'll read
up on ways to get around that. The Selection.MoveDown and align center
is a very simple way of centering the picture/object that follows the
Table Caption that was just created. I haven't come up with a better
way to do this yet. I'll keep working on it and thanks again for the
suggestions!

Brandon
 

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