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
'69 Camaro - 31 Aug 2007 06:03 GMT
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.
Allen_N - 31 Aug 2007 06:48 GMT
Thanks, Gunny.
I'll have to start hunting for the definition in my other MDBs and ADPs.
> Hi, Al.
>
[quoted text clipped - 20 lines]
> http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
> info.
'69 Camaro - 31 Aug 2007 07:04 GMT
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.