Link to home
Start Free TrialLog in
Avatar of RAFAAJ
RAFAAJ

asked on

Prevent the start of an Application while working in Excel !!

Hi,

How can I stop the user from launching an application ( let's say NotePad) while he is working in Excel ?

So if the user strats NotePad , I want this to be detected by Excel and shuts NotePad immediatly and then display a MsgBox to the user informing them that they cannot have NotePad open while working in Excel.

Any thoughts would be much appeciated.

Thanks.
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

'Module code

Option Explicit
Public Declare Function SendMessageLong& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Const WM_CLOSE = &H10
Public Sub DetectWindow(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Dim n As Long
n = FindWindow("notepad", vbNullString)
   
    Select Case n
        Case 0
    Exit Sub
        Case Is > 0
        Call SendMessageLong(n, WM_CLOSE, 0&, 0&)
        MsgBox "You can't have notepad open when using excel"
    End Select
End Sub


'Form code
Private Sub Form_Load()
SetTimer Me.hwnd, 0, 1000, AddressOf DetectWindow
End Sub

Private Sub Form_Unload(Cancel As Integer)
KillTimer Me.hwnd, 0
End Sub
Avatar of RAFAAJ

ASKER

egl1044,

Thanks very much for the code .

Two things :

* When Notepad is closed , the MsgBox flashes on the TaskBar instead of coming to the Forground. I have used the SetForegroundWindow API but that doesn't work...Is there a fix to this ?

* Is it possible to use a WH_CBT Hook or another type of hook instead of a Timer ? I think it would be much more efficient .

Anyway this is your code adapted so far :

Code:


' In a Standard (Bas) Module.

Option Explicit

Declare Function SendMessageLong& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Const WM_CLOSE = &H10
Dim lngTimerID As Long
Dim lngTimerEnabled As Boolean

Public Sub DetectWindow(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Dim n As Long
n = FindWindow("notepad", vbNullString)
   
    Select Case n
        Case 0
    Exit Sub
        Case Is > 0
        Call SendMessageLong(n, WM_CLOSE, 0&, 0&)
        MsgBox "You can't have notepad open when using excel"
        SetForegroundWindow Application.hwnd
    End Select
End Sub

 Sub StartWatching()
 
    If lngTimerEnabled Then
        MsgBox "TIMER ALREADY ON !"
        Exit Sub
    End If
   
    lngTimerEnabled = True
    lngTimerID = SetTimer(0, 0, 1, AddressOf DetectWindow)

End Sub

Sub StopWatching()
    lngTimerEnabled = False
    KillTimer 0, lngTimerID
End Sub


Any ideas ?


Regards.










SetTimer and KillTimer will work fine.  Even a standard Timer will work just fine.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial