VBA! Can't get process to end
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: VBA! Can't get process to end

  1. #1
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    132

    VBA! Can't get process to end

    Hi everyone,

    I have this piece of code that unpacks a zipfile
    shellandwait ("C:\Wzunzip C:\test.zip C:\")

    After this process is done I want to delete it with
    Kill "C:\test.zip"

    The problem is that I can not delete the zipfile because the Wzunzip process won't end. The Shellandwait funtion ensures the unpack line finishes first before resuming next code. I know this works becasue there are a few other command in between these 2 commands wich I could not do if the shellandwait function wouldn't work properly.

    So here is the question:
    How can I end the Wzunzip.exe process so I can delete that horrible zip file afterwords? Oh, I work with VBA.
    Thank you for your time and effort.

    Best regards,

    Jasper
    Last edited by Jasper XAFAX; April 22nd, 2004 at 04:32 AM.
    Flying is to throw yourself on the ground and miss

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    if zip exe has a window (else you will have to enum modules...), you can try:
    Code:
    Option Explicit
    
    
    Const WM_CLOSE = &H10
    Private theCollWin As Collection
    
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    
    
    
    Sub Main()
        Set theCollWin = New Collection
        theCollWin.Add "at least partial Name of window"
            EnumWindows AddressOf EnumWindowsProc, ByVal 0&
    End Sub
    Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
        Dim sSave As String, Ret As Long
        Dim theItem As Variant
        Dim Thread_id As Long
        Dim ThePid As Long
        Ret = GetWindowTextLength(hwnd)
        If Ret > 0 Then
            sSave = Space(Ret)
            GetWindowText hwnd, sSave, Ret + 1
            For Each theItem In theCollWin
                 
                If InStr(1, sSave, theItem, vbTextCompare) > 0 Then
                    'close window
                    SetForegroundWindow hwnd
    
                    SendMessage hwnd, WM_CLOSE, 0, 0
    ' Open App.Path & "\log.txt" For Append As #1
    ' Print #1, sSave
    ' Close #1
                End If
            Next
        End If
        'continue enumeration
        EnumWindowsProc = True
    End Function
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    132
    Hi Cimperiali,

    Thank you for your time and effort to reply.
    I do not know if the code you posted is the code I need because I do not fully understand it. Before I go and study this code (I like to know how al of the code works in my project) I would like to verify if it is code I need.
    The app Wzunzip will close after it has done it's job but it will stay active in the background. It is only vissible active in the taskmanager. If I end the proces in the taskmanager I can delete the zip file.

    Best regards,

    Jasper
    Flying is to throw yourself on the ground and miss

  4. #4
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    132
    Cimperiali,

    I found the problem. The Shellandwait function is causing it. I will have to sort that out. Really strange the function has worked with every app exept this on. Proves that I can't take everything for granted.

    Best regards,

    Jasper
    Flying is to throw yourself on the ground and miss

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Closing a process:
    You might foind some infos here:
    (scroll down)
    http://www.codeguru.com/forum/showth...hreadid=291721
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Adapting codes found on web: how to stop process

    Here a way to stop process.
    Input process name as you see it via task manager....
    Attached Files Attached Files
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    ShellAndWait ...

    ... do you have the one that uses integers? Change them
    all to long...
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  8. #8
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    132
    Hi Cimperialy,

    Sorry I did not respond to you replies. I was ill for a while. I will look into your efforts as soon as humanly possible.

    As I know from times before your posts always have helped me. Thank you very much for time and effort. Your the next best thing after canned beer.

    Best regards,

    Jasper
    Flying is to throw yourself on the ground and miss

  9. #9
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Originally posted by Jasper XAFAX
    [...]Your the next best thing after canned beer[...]
    LOL!

    Thanks for kind words
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center