By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,775 Members | 2,658 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,775 IT Pros & Developers. It's quick & easy.

How to terminate process with VBA?

P: n/a
I use the default windows phone dialer to make calls from an Access form. I
close the phone dialer after the call is made using the below function. The
problem is I'm not closing the Phone Dialer application properly which
results in the Phone Dialer icon remaining in the system tray. I've tried
using the Windows API to refresh the system tray, but could not get that to
work.

How to I gracefully close the phone dialer application with VBA?

Private Function CloseDialer(strDialer As String) 'note: strDialer =
"dialer.exe"
Dim objProcList As Object
Dim objWMI As Object
Dim objProc As Object
Set objWMI = GetObject("winmgmts:") 'create WMI object instance
If Not IsNull(objWMI) Then
Set objProcList = objWMI.InstancesOf("win32_process") 'create object
collection of Win32 processes
For Each objProc In objProcList 'iterate through the enumerated
collection
If UCase(objProc.Name) = UCase(strDialer) Then objProc.Terminate
(0)
Next
End If

Thanks in advance.
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I checked Randy Birch's site, http://vbnet.mvps.org/, but didn't find
an answer. You might want to post in the VB newsgroup... maybe
someone else knows or I just missed it...
Nov 13 '05 #2

P: n/a
> I checked Randy Birch's site, http://vbnet.mvps.org/, but didn't find
an answer. You might want to post in the VB newsgroup... maybe
someone else knows or I just missed it...


Thanks for the tip. I think I need the window title in order to close it
properly (with the windows API). Now that I think about it, that's why I
originally wrote the code to just kill the process - there's no title on the
phone dialer window.
Nov 13 '05 #3

P: n/a
You do not need the WIndow Title just the Window's class name will do.
Deko is this the same issue I helped you with back in April?
--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"deko" <de*****@hotmail.com> wrote in message
news:vO***************@newssvr27.news.prodigy.com. ..
I checked Randy Birch's site, http://vbnet.mvps.org/, but didn't find an answer. You might want to post in the VB newsgroup... maybe
someone else knows or I just missed it...
Thanks for the tip. I think I need the window title in order to close

it properly (with the windows API). Now that I think about it, that's why I originally wrote the code to just kill the process - there's no title on the phone dialer window.


Nov 13 '05 #4

P: n/a
> Deko is this the same issue I helped you with back in April?

You're correct (and I'm a bit embarrassed), you did help me with this
before. For some reason I had trouble with the code you posted in April and
I settled for a less than graceful termination of the dialer (which resulted
in the dialer icon remaining in the systray).

I'm now revisiting this module to make sure numbers are in canonical format
and also wanted to get rid of the extraneous dialer icon in the systray. I
forgot about our previous conversation.

In any case, I've gone back and reviewed the code you previously
recommended - and got it to work this time. But I'm having trouble
eliminating the "Are you sure?" dialog that appears when the code attempts
to close the dialer. Is there a way to send this dialog a "Yes" response?
(I sent you a working mdb)

Here's the code:

Option Compare Database
Option Explicit
Const WM_CLOSE = &H10
Const SW_HIDE = 0
'API use default Windows Phone Dialer
Private Declare Function tapiRequestMakeCall _
Lib "TAPI32.DLL" _
( _
ByVal DestAddress As String, _
ByVal AppName As String, _
ByVal CalledParty As String, _
ByVal Comment As String _
) _
As Long
'API sleep to give user time to pick up phone
Private Declare Sub Sleep _
Lib "kernel32" _
( _
ByVal dwMilliseconds As Long _
)
'API find application by full caption
Private Declare Function FindWindow _
Lib "user32" _
Alias "FindWindowA" _
( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) _
As Long
'API bring Window to foreground
Private Declare Function SetForegroundWindow _
Lib "user32" _
( _
ByVal hwnd As Long _
) _
As Long
'API send message to application
Private Declare Function PostMessage _
Lib "user32" _
Alias "PostMessageA" _
( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) _
As Long

Private Function CloseDialer(AppCaption As String)
Dim hwnd As Long
hwnd = FindWindow(vbNullString, AppCaption)
If hwnd <> 0 Then
'Bring to Front
SetForegroundWindow hwnd
'Close the app nicely
PostMessage hwnd, WM_CLOSE, 0&, 0&
'"Are you sure" dialog is received here
' how to send "Are you sure" dialog a Yes response?
'FindWindow ("Dialer") ???
'PostMessage hwnd, "Yes" ???
End If
End Function

Private Sub Telephone_Numbers_DblClick(Cancel As Integer)
On Error GoTo HandleErr
Dim objDialer As Object
Dim strMsg As String
Dim varReturn As Long
Dim intX As Integer
Dim varDialWait As Variant
Dim strDial As Variant
Dim j As Variant
Dim lngRetVal As Long
Dim i As Integer
Dim n As String
Dim p As String
Static blnDialing As Boolean 'prevent reentry into sub while dialing
DoEvents
If blnDialing Then Exit Sub
strDial = Trim$(Me!TelNumber)
For i = 1 To Len(strDial)
n = Mid(strDial, i, 1)
If n Like "#" Then p = p & n
Next i
strDial = "+1" & Format(Left(p, 10), "(@@@)&&&-&&&&")
lngRetVal = tapiRequestMakeCall(strDial, "", "", "faxback")
If lngRetVal = 0 Then
blnDialing = True
For intX = 1 To 8 'user has 8 seconds to pick up phone
DoEvents
Sleep 1000
Next
End If
Exit_Here:
On Error Resume Next
blnDialing = False
CloseDialer ("Phone Dialer")
Exit Sub
HandleErr:
Select Case Err.Number
Case 94 'Invalid Use of Null - if double-click on blank
'in this case, just open the dialer
Set objDialer = CreateObject("Scripting.FileSystemObject")
If objDialer.FileExists("c:\Program Files\Windows
NT\dialer.exe") Then
Call Shell("c:\Program Files\Windows NT\dialer.exe",
vbNormalFocus)
ElseIf objDialer.FileExists("c:\Windows\dialer.exe") Then
Call Shell("c:\Windows\dialer.exe", vbNormalFocus)
End If
Set objDialer = Nothing
Exit Sub
Case Else
'modHandler.LogErr ("frm0Telephone"),
("Telephone_Numbers_DblClick")
End Select
Resume Exit_Here
End Sub
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.