I have Tiscali 'unlimited' broadband, so I thought I could use as much bandwidth as I wanted. All I was doing was using BA a lot. I recently got an e-mail from them saying I was in breach of their fair-use policy, and unless I cut down the usage during peak times they would make me share bandwidth with other 'heavy users'. A lot of the time I'm not home when I'm finished using BA for the day so I set about finding how to close it when I wasn't there.
AppActivate and Sendkeys worked to a degree but had a few problems, then I found out about a load more stuff about Excel that I hadn't seen before, namely the Windows API functions.
Anyway, the following code solved my problem. For those who know vba and want to use it, you copy the whole code into a module and call the Close_By_Caption function with the exact caption in the title bar of the window you want to close as the argument in speech marks. ie.
- Code: Select all
Close_By_Caption "TitleBarCaptionHere"
If anyone who doesn't understand vba wants to use it, just post here with the details of your worksheet (just if it's formulae only or has vba in it) and how you want to use it and I'll sort something out for you.
- Code: Select all
'API Find applcation 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, _
lParam As Any _
) _
As Long
Const WM_CLOSE = &H10
Function Close_By_Caption(AppCaption As String)
Dim hwnd As Long
hwnd = FindWindow(vbNullString, AppCaption)
If hwnd Then
SetForegroundWindow hwnd 'Bring to Front
PostMessage hwnd, WM_CLOSE, 0&, 0& 'Close the app nicely
End If
End Function