Interpretation, please... (Declare Sub)

Z

zSplash

I don't know exactly why/how this sub works, but it does. (Thanks for
giving it to me, guys.)

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

To further my understanding, could someone please explain exactly what the
terminology means (i.e., "Declare Sub" versus "Sub" and "Lib "kernel32""),
and why it has to be declared first-off in the module?

TIA
 
J

Jay Freedman

Hi z,

The Declare keyword says you're going to refer to a routine that's
external to VBA, in a "dynamic link library" (DLL). You can declare a
Sub or a Function -- a Function returns a value, while a Sub doesn't.

The "Lib kernel32" gives the name of the DLL that contains the sub or
function. In this case it's kernel32.dll, which is part of Windows and
lives in the folder C:\Windows\system32.

The declaration is required to be at the module level -- that is,
before any Sub or Function -- so the VBA interpreter can look up the
DLL and get the address of the sub or function inside it before your
code calls it.

All this and more is explained in the VBA help topic on the Declare
statement.

Although the Declare statement can work with any DLL, including ones
that you or others create in VB 6 or another compiled language, it's
most often used with DLLs like kernel32 that come with Windows. These
are referred to collectively as the Win32 API (application programming
interface). An excellent source of information and sample code using
the API is Karl Peterson's site http://vb.mvps.org/samples/.

If you get the hang of it and it gets your creative juices flowing,
you should look for a copy of Dan Appleman's "Visual Basic
Programmer's Guide to the Win32 API". It's only about 1500 pages. :)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
Z

zSplash

Thanks, Jay, again.

I love this VBA stuff, but feel like I'm crossing the ocean on a few little
stepping stones (my limited knowledge) until you guys come cruising along
and throw me a life-preserver that keeps me hanging on 'til I find another
rock. Uff-dah. So much to learn, so little time...

Thanks for the great explanation and direction!

st.
 

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