problem with DetectExcel

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Previously, I have used the following code (from a book) successfully. In my
current MDB, however, the call to FindWindow raises the error: "Sub or
Function not defined". I don't remember ever having to explicitly declare Win
API functions from within VB or VBA. Am I missing a reference?

Thanks!
--Al


Sub DetectExcel()

' Detects a running instance of Excel and registers it.

' pre-25/06/07.

Const WM_USER = 1024
Dim hwnd As Long

' If Excel is running this API call returns its handle.

hwnd = FindWindow("XLMAIN", 0)
If hwnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hwnd, WM_USER + 18, 0, 0
End If

End Sub
 
Hi, Al.
the call to FindWindow raises the error: "Sub or
Function not defined".

If the FindWindow( ) function is an alias for the FindWindowA API function,
then the function must be declared in one of the following ways:

1.) Publicly, if the function is called from procedures located in other
modules, or
2.) Privately or publicly, if the function is called from procedures within
the same module.

Otherwise, FindWindow( ) must be defined as a user-defined function. It's
not a built-in function.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
Blogs: www.DataDevilDog.BlogSpot.com, www.DatabaseTips.BlogSpot.com
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.
 
Thanks, Gunny.

I'll have to start hunting for the definition in my other MDBs and ADPs.
 
Hi, Al.
I'll have to start hunting for the definition in my other MDBs and ADPs.

All the declarations I could find use a string as the second argument, not a
Long data type:

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal strClass As String, _
ByVal lpWindow As String) As Long

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
Blogs: www.DataDevilDog.BlogSpot.com, www.DatabaseTips.BlogSpot.com
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.
 

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

Back
Top