Private Function fIsAppRunning() As Boolean
'Looks to see if Lotus Notes is open
Dim lngH As Integer
Dim lngX, lngTmp As Integer
Const WM_USER As Short = 1024
On Error GoTo fIsAppRunning_Err
fIsAppRunning = False
lngH = FindWindow("NOTES", vbNullString)
If lngH <> 0 Then
SendMessage(lngH, WM_USER + 18, 0, 0)
lngX = IsIconic(lngH)
If lngX <> 0 Then
lngTmp = ShowWindow(lngH, 1)
End If
lngTmp = SetForegroundWindow(lngH)
fIsAppRunning = True
End If
fIsAppRunning_Exit:
Exit Function
fIsAppRunning_Err:
fIsAppRunning = False
Resume fIsAppRunning_Exit
End Function
Refactorings
No refactoring yet !
rikkus
September 25, 2008, September 25, 2008 16:56, permalink
This kind of error handling is idiomatic in VB.
hcase
September 25, 2008, September 25, 2008 23:33, permalink
My own refactoring since noone seems interested.
Private Function IsAppRunning() As Boolean
'Looks to see if Lotus Notes is open
Dim lngH As Integer
Dim lngX, lngTmp As Integer
Const WM_USER As Short = 1024
IsAppRunning = False
Try
lngH = FindWindow("NOTES", vbNullString)
If lngH <> 0 Then
SendMessage(lngH, WM_USER + 18, 0, 0)
lngX = IsIconic(lngH)
If lngX <> 0 Then
lngTmp = ShowWindow(lngH, 1)
End If
lngTmp = SetForegroundWindow(lngH)
IsAppRunning = True
End If
Catch x As Error
IsAppRunning = False
End Try
End Function
https://me.yahoo.com/a/gRMed8kNn9bseqntTCs6dXkdMWMkQus-
February 16, 2011, February 16, 2011 12:52, permalink
You don't need to set IsAppRunning, just use return true or false. It makes a bit more sense for someone reading it. I believe you also want to catch the system.exception as well - http://msdn.microsoft.com/en-us/library/system.exception.aspx. Hope this helps a little bit.
Wade
Private Function IsAppRunning() As Boolean
'Looks to see if Lotus Notes is open
Dim lngH As Integer
Dim lngX, lngTmp As Integer
Const WM_USER As Short = 1024
Try
lngH = FindWindow("NOTES", vbNullString)
If lngH <> 0 Then
SendMessage(lngH, WM_USER + 18, 0, 0)
lngX = IsIconic(lngH)
If lngX <> 0 Then
lngTmp = ShowWindow(lngH, 1)
End If
lngTmp = SetForegroundWindow(lngH)
return True
End If
Catch x As Exception
return False
End Try
End Function
This horrible code I inherited uses a GOTO. Anyone care to fix it?