Closing another Application problem

G

Guest

Hi folks,

I found the following code from the web. I pase it in Access xp module.
When I ran, it gave me error message. I tried to use it for closing Excel
which had error message pop up. Could anyone help me to fix the code?

Thanks in advance.

' Closing another Application
' *************************************************************

' ***************** The command button would look like this:
Private Sub Command1_Click()
Close_External_Application "Microsoft Word"
End Sub

' *********************************************************
'compat:vb3,vb4-16
'Close_External_Application
' Description:To have a program programmatically close anoth
' er program, use this code
' By: VB Pro'
' Inputs:strCaptionTitle--caption title of window to close
' Returns:true on success, else false
' Assumes:None
' Side Effects:None'
' Close an existing program
' *********************************************************

Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal _
lpWindowName As Any) As Integer
Declare Function GetWindowTask Lib "User" (ByVal hWnd As Integer) As Integer
Declare Function PostAppMessage Lib "User" (ByVal hTask As Integer, ByVal
wMsg _
As Integer, ByVal wParam As Integer, lParam As Any) As Integer
Public Const WM_QUIT = &H12


Function Close_External_Application(ByVal strCaptionTitle As String) _
As Boolean
Dim intWindowHandle As Integer
Dim intTaskHandle As Integer
Dim intPostReturnValue As Integer


' *************** set defaults
Close_External_Application = False

' *************** get handle of window matching caption
intWindowHandle = FindWindow(0&, strCaptionTitle)

If (intWindowHandle <> 0) Then
' *************** window found
intTaskHandle = GetWindowTask(intWindowHandle)
intPostReturnValue = PostAppMessage(intTaskHandle, WM_QUIT, 0, 0&)

' *************** set return value
Close_External_Application = True
End If
End Function
 
B

Brendan Reynolds

Those are 16-bit API calls, designed for Windows 3.x, and not compatible
with 32-bit versions of Windows (Windows NT 4, Windows 95, and later). If
you go back to the site where you got the code, there may well be a 32-bit
version there. If not, you'll find 32-bit versions of most if not all of
those API calls in the API section at www.mvps.org/access.

Anything you can't find there, you'll find in ApiViewer which you can
download (it's free) from
http://www.activevb.de/rubriken/apiviewer/index-apiviewereng.html
 
G

Guest

Hi Alex/Brendan,

Thanks very much for your help. You save my life.

Thanks a lot.

Tim.
 

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