Difference between running and stepping makro

J

Jan Eliasen

Hi

I have written a macro for Word that fills text from textfiles into
specific places in my word-document.

My problem is, that if I step through the code in the debugger, then
everything works just fine, but if I run the macro then it fails.

My document looks like this:

2.1 Headline
2.1.1 Subheadline
2.1.2 Subheadline 2

2.2 Headline 2

2.3 Headline 3

2.4 Headline 4

My problem is that it is only in 2.4 it doesn't work. The output is
supposed to be like this:
2.4 Headline 4
text, more text and a lot of more text

But the output is actually something like
2.4
2.5 text, more tet and a lot of more text

2.6 Headline 4

But only when running the macro and not when I step through it. Does
anyone have any ideas?

Thanks in advance!
 
J

Jan Eliasen

it may sometimes be necessary to wait a second or two
after starting an operation on files like open, close
etc. I can't see any other reason here. If you need
a sub wait(x seconds or so), to experiment with,
let the community know.
Hi

I tried adding DoEvents all over the place, but that didn't help. I
can't find a built in wait function, so it would be great if you have
one.

Thanks.
 
H

Helmut Weber

Hi Jan,
i'm using this construction, counting seconds since
1st of January 1900, it is, I think.
Therefore it runs over midnight and over new year,
and would make it theoretically possible, to wait
for weeks or years. The often recommended API-sleep,
AFAIK, halts all processes, which is sometimes
undesirable.
Public Sub Wait(Pause As Integer)
Dim TimeLo As Double
Dim TimeHi As Double
TimeLo = YearSecs
TimeHi = TimeLo + Pause
Do While TimeLo < TimeHi
DoEvents
TimeLo = YearSecs
Loop
End Sub
Public Function YearSecs() As Double
' seconds since 1900
Dim SecNow As Double
Dim Daynow As Double
Dim Daysec As Double
SecNow = CLng(Timer)
Daynow = CLng(CDate(Now))
Daynow = Daynow * 86400
Daysec = Daynow + SecNow
YearSecs = Daysec
End Function
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT 4.0
 
J

Jan Eliasen

i'm using this construction, counting seconds since
1st of January 1900, it is, I think.
Thanks...

The function YearSec, does it do more than the built in "timer" does?
 
H

Helmut Weber

Hi,
"timer" returns the seconds since midnight.
This may cause problems, when start time is
before midnight and end time after midnight.
Including days or weeks or months in the calculation
does not solve all possible problems, one has to refer
to the greatest time unit, year.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT 4.0
 
J

Jan Eliasen

"timer" returns the seconds since midnight.
This may cause problems, when start time is
before midnight and end time after midnight.
Including days or weeks or months in the calculation
does not solve all possible problems, one has to refer
to the greatest time unit, year.
Right... And silly me for not reading the documentation properly!
*kick*
 
K

Klaus Linke

Helmut Weber said:
Hi,
"timer" returns the seconds since midnight.
This may cause problems, when start time is
before midnight and end time after midnight.
Including days or weeks or months in the calculation
does not solve all possible problems, one has to refer
to the greatest time unit, year.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT 4.0


Hi Helmut,

So you are running macros around midnight, too?

;-) ... If you want to make sure, you could add
myElapsedTime=Timer-myStartTime
If myElapsedTime < 0 then
myElapsedTime=myElapsedTime+24&*60*60
End If

(...assuming your macros don't run longer than a day, that is...)

Greetings from Stuttgart,
Klaus
 
M

Martin Seelhofer

Hi there
myElapsedTime=Timer-myStartTime
If myElapsedTime < 0 then
myElapsedTime=myElapsedTime+24&*60*60
End If

(...assuming your macros don't run longer than a day, that is...)

Timer() is not useful for long running macros. Use the Now()-
function instead, or combine the two. [Those fractions of a second
don't really make the coffee hot, do they ;-)]

Dim startTime As Date
Dim elapsedTime As Date
startTime = Now()
....
elapsedTime = Now()-startTime
MsgBox elapsedTime


Cheers,

Martin
 
H

Helmut Weber

Hi Klaus,
the code lines are from a multi-purpose time control
written in VB. It runs endlessly (or up to the limit
of double), or until the next crash. ;-)
I use it to start routines every 30th day,
every week, daily, once an hour and so on. Some of the
routines may last for hours.
It certainly looks kind of odd to count seconds
since 1900 in order to wait a moment.
Greetings from Landsberg am Lech
Helmut Weber
 
K

Klaus Linke

It runs endlessly [...] or until the next crash. ;-)


Hi Helmut, Martin,

That would be much less than a day for me, usually ;-P

I use the Timer for checking the performance of macros only.
Now() would be too crude for that, I think.

Greetings,
Klaus
 

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