Closing/ending an external program

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
I know I can start an external program by using the Shell command, but how can I *end* the program?

I'm assuming something to do with API, which I have never used. I've searched and searched, and can't seem to find any already-written code that works. You would think someone, somewhere would have asked this already but if they have, it is very well hidden. All I keep finding is code to close instances of Excel or run a program and then continue a macro after the shell program ends, neither of which are the scenario that applies here. Actually, I take that back. I found this newsgroup post which is asking the exact same thing but the code posted does not work. I've also skimmed www.allapi.net but I can't figure out how to put the code together myself.

All I'm trying to do is start VBScroll when Excel opens and end it when Excel closes.

VBScroll simply sits in the taskbar; I have to right-click the icon and select Exit from the menu to close it. Current specs would be Excel 2002, Windows 2000. I was figuring on just adding the code to Personal.xls, nothing fancy. It's easy enough to start VBScroll with the shell command, no problems there. I just can't figure out how to end it.

Any <s>delicious cookies,</s> ideas, links, what have you?
 
Hmmm.

When I ran the code manually, VBScroll closed, no problem.

Now that I'm trying it with the code in the Personal.xls, nothing is happening. The program is still open after closing Excel. :-?

What is the difference? It's the exact same code. The only difference is that I'm running it in the ThisWorkbook module of Personal.xls:

Code:
Public IDvbscroll As Long

Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Declare Function OpenProcess Lib "kernel32" _
  (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
   ByVal dwProcessId As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
   (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" _
   (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
' ---------------------
Private Sub Workbook_Open()
Application.CommandBars("Reviewing").Visible = False

'starts VBScroll program
IDvbscroll = Shell("C:\Program Files\VBScroll\VBScroll.exe")
End Sub
' ---------------------

Private Sub Workbook_Close()
'ends VBScroll program
EndShelledProcess IDvbscroll
End Sub
' ---------------------

Public Function EndShelledProcess(ShellReturnValue As Long) _
   As Boolean

'PURPOSE: End a process started with VB's Shell Statement
'INPUT: Task ID returned by Shell
'RETURNS: True if succesful, false otherwise

On Error Resume Next

Dim hInst As Long
Dim hProcess As Long
Dim lExitCode As Long
Dim lRet As Long

hInst = ShellReturnValue
If hInst = 0 Then Exit Function

'Get handle to process
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, hInst)
If hProcess <> 0 Then
    'get exit code
    GetExitCodeProcess hProcess, lExitCode
        If lExitCode <> 0 Then
                'bye-bye
            lRet = TerminateProcess(hProcess, lExitCode)
            EndShelledProcess = lRet > 0
        End If
End If

End Function

I even just tried it with the function in a separate module instead of in the ThisWorkbook module. Same thing--vbscroll is still running after Excel is closed.

...I'm confused as to why. :huh: :-?

Edit: I may not get back to this very quickly--I'm off to do some sort of actual work, now. I might try it later tonight at home with Excel 2003, as well.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi Norie. Not my code. I copied and pasted it from the link in my first reply. I don't have any API books. http://www.allapi.net/ is a great site as well as MSDN. Most of the examples on MSDN are in C, but you will understand the syntax for the most part. api.net has several great, free downloads.
 
Upvote 0
Tom

Thanks for the link, I'll take a look.:)

I swear I used to have a pretty comprehensive API guide that I downloaded from somewhere.

It might have actually been a whole website that I downloaded, but I just can't seem to find it.:o

Looks as though I'll just have to rely on online resources and the API Viewer.
 
Upvote 0
Kristy

Cheers, I hadn't had a look at Tom's link yet.:oops:

I am now, but the guide seems to be taking some time to download.

But the screenshots look pretty good.:)
 
Upvote 0
Kristy.

Change:
Private Sub Workbook_Close()
To:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

If that does not work, rename personal.xls and or place this sub within a standard module within your personal.xls workbook...

Sub Auto_Close()
Call ThisWorkbook.EndShelledProcess(ThisWorkbook.IDvbscroll)
End Sub
 
Upvote 0
Kristy

Thanks very much for that link, I don't know why but when I tried to download it the first time it totally locked my computer up.

After restart it downloaded and installed in under 30 seconds.

And it looks pretty impressive too.

I really need to upgrade.:)
 
Upvote 0
Tom

Is that any different from the standard API Viewer?

The interface looks different but it doesn't look like the data is.

Anyways, I'll probably download it anyway.

I'm just download crazy these days.:)
 
Upvote 0
It is better than the standard API Viewer. It has plugins, is updated more often, contains many of the contstants (and their assignements) that are sometimes hard to find for us VB people
 
Upvote 0

Forum statistics

Threads
1,225,477
Messages
6,185,210
Members
453,283
Latest member
Shortm88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top