Make VBA wait for return from external function

G

Gib Bogle

When a function in a DLL (maybe created with C or Fortran) is called
from VBA, execution of the VBA code continues without waiting for the
called function to return. Is there a way to make VBA wait?
 
P

Peter T

Search this group for "Shell and Wait". There are various approaches, which
will depend on what you are doing overall.

Regards,
Peter T
 
G

Gib Bogle

Peter said:
Search this group for "Shell and Wait". There are various approaches, which
will depend on what you are doing overall.

Regards,
Peter T

I did that, and found only your post.
I would have thought that "Shell" applied to executing an external
process rather than to a call to a DLL routine. Am I wrong?
 
P

Peter T

I did that, and found only your post.

So it does, sorry about that, try "ShellAndWait"
I would have thought that "Shell" applied to executing an external process
rather than to a call to a DLL routine. Am I wrong?

Yes you are right and perhaps I misunderstood your situation. Reading your
OP again I don't understand why there is such a problem calling your dll.
Normally when VBA (or an Excel formula) calls a function, even an external
function in a dll, code waits until the function returns its result.

Regards,
Peter T
 
P

Peter T

PS
Are you sure there is nothing in the the dll function that leads to
asynchronous behaviour

Regards,
Peter T
 
G

Gib Bogle

Peter said:
PS
Are you sure there is nothing in the the dll function that leads to
asynchronous behaviour

Regards,
Peter T

To be honest this is not my problem. A poster asked this question on a
Fortran forum, and my curiosity was provoked. The poster says that
"...Excel 2007 VBA skips over calls [to his Fortran DLL] that take any
time to execute. If I go into debug mode for the VBA and pause after
the call the code completes and the results are passed into Excel."
This doesn't eliminate the possibility that there is something wrong
with the way he handles the returned results when not in debug mode.

Gib
 
H

Harald Staff

Gib Bogle said:
To be honest this is not my problem. A poster asked this question on a
Fortran forum, and my curiosity was provoked. The poster says that
"...Excel 2007 VBA skips over calls [to his Fortran DLL] that take any
time to execute. If I go into debug mode for the VBA and pause after the
call the code completes and the results are passed into Excel."

If it works stepping and pausing through, a simple
DoEvents
usually does the trick when you want to wait for a calculation or similar.
(Not including full loading of webpages and such demanding tasks)

X = MyBogusFunction(y)
DoEvents
Range("A1").Value = X

HTH. Best wishes Harald
 
G

Gib Bogle

Harald said:
Gib Bogle said:
To be honest this is not my problem. A poster asked this question on
a Fortran forum, and my curiosity was provoked. The poster says that
"...Excel 2007 VBA skips over calls [to his Fortran DLL] that take any
time to execute. If I go into debug mode for the VBA and pause after
the call the code completes and the results are passed into Excel."

If it works stepping and pausing through, a simple
DoEvents
usually does the trick when you want to wait for a calculation or
similar. (Not including full loading of webpages and such demanding tasks)

X = MyBogusFunction(y)
DoEvents
Range("A1").Value = X

HTH. Best wishes Harald

Thanks, I'll pass that advice on.
 

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