Announcement

Collapse
No announcement yet.

VBA: Close program

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBA: Close program

    I am having difficulty closing a external program from within xcel.
    I have a macro that opens one of two programs that import data from the com port. There are times when I need to close one program and open the other.
    The code I use to open the program is:

    Sub OpenDetails()
    '
    ' OpenDetails Macro

    ' Keyboard Shortcut: Ctrl+s
    '
    On Error Resume Next
    RetVal = Shell("C:\Program Files\METTLER TOLEDO\BalanceLink\BAL32.exe")
    Application.Wait (Now + TimeValue("00:00:02")) 'Give time to load
    AppActivate Application.Caption

    Sheets("Detail").Visible = True
    Sheets("Instructions").Visible = False
    Range("C2").Select


    End Sub
    Can anyone help please?

  • #2
    Hi Johnc and welcome,

    Try this example, which opens and then closes NotePad.
    Code:
    Sub CloseNotePad()
        If Range("TaskID").Value > 0 Then
            ' close know copy of note pad
            AppActivate Range("TaskID").Value
            Application.SendKeys "%{F4}", True
        End If
    End Sub
    Sub LoadNotePad()
        ' open Notepad and return program's task ID
        Range("TaskID").Value = Shell("C:\notepad.EXE", 1)
    End Sub
    You will need to change the path in the Shell command.

    The Range("TaskID") is a single cell named range.

    Hopefully you can adapt this to suit your application.

    Cheers
    Andy

    Cheers
    Andy

    Comment

    Working...
    X