Word 2003 crashes with 2003 vba macro

M

marcelk

Hi,

I'm testing all the macros our company is using. They are build under Word
2003 and now I'm testing them in Word 2007. A lot is working as it should
however in one particual case the vba code behaves strange. In the next vba
code I add every time a paragraph and checks if the cursor is on the right
level. When running the macro, Word is hanging and the only solution is to
quit the process. When I run the macro in debug mode and step through the
code with F8 it works as expected. Is there a kind of timing problem? I added
some debug statements to see where it stops.

Public Sub AlineasInvoegenTotAanRegel(n As Integer)
Dim i As Integer, pos As Single, pos1 As Integer

Debug.Print "17n1"

pos = Selection.Information(wdVerticalPositionRelativeToPage)
pos1 = Round(pos / 13)

Do While pos1 < (n - 1)
Debug.Print "n: " + n
Debug.Print "17n2"
Selection.TypeParagraph
pos = Selection.Information(wdVerticalPositionRelativeToPage)
pos1 = Round(pos / 13)
Debug.Print "Pos: " + Str(pos)
Debug.Print "Pos1: " + Str(pos1)
Loop

Debug.Print "17n3"

End Sub
 
P

Pesach Shelnitz

Hi Marcel,

One of the ways in which a macro can cause Word to hang is by having a loop,
such as a Do While loop with a condition that never because false. Your macro
will exit the loop in it only when pos1 becomes greater than or equal to n -
1. As the cycles of the loop proceed, pos1 increases until the cursor is
pushed onto the next page. Thus, it is conceivable that it will never reach
the value of n - 1.

The difference in behavior that you are seeing between the different
versions of Word may be due to differences in the paragraph formatting of the
text on which you are testing this macro in the different versions.

I or someone else who monitors this newsgroup might be able to help you more
if you would answer the following questions.

What is the value of n that is passed to this subprocedure?

What is the line spacing of the text on which you tested the macro in the
two versions of Word?

What is the name of the font and what is the font size of the text on which
you tested the macro in the two versions of Word?
 
M

marcelk

Hi Pesach,

Thank you for the reply. Yes, I found out that the loop in this case never
reach the value to terminate the loop, however it doesn't make clear why
there is a difference between the F8 step by step (the program gets time so
settle) and the run mode with the same document and the same steps. The macro
is part of a bigger program. It creates headers and footers and the user can
chose to use new headers and footers or use them from the previous section.
In the case the user chose for a new header and footer this code runs
erratic. I haven to check the differences between them. It seems that Word
2007 handles this different than Word 2003.
 

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