excelvbasketchup

Close sketchup from Excel


I am opening sketchup from excel, then running a plugin, I use the below code to open sketchup, then use SendKeys to select the plugin.

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Sub fLaunchProgram(ByVal sProgram As String)
    Dim ProcessHandle As Long
    Dim ProcessId As Long
    On Error GoTo errExit
    ProcessId = Shell(sProgram, vbNormalFocus)
    ProcessHandle = OpenProcess(&H1F0000, 0, ProcessId)
    WaitForSingleObject ProcessHandle, INFINITE
    Exit Sub

errExit:
    MsgBox "External program " & sProgram & " was not found", vbCritical, " fLaunchProgram"
End Sub
'To load SU
Sub pTest()
    fLaunchProgram ("C:\Program Files\SketchUp\SketchUp 2015\SketchUp.exe")
End Sub

Sub Button1_Click()

    pTest
    SendKeys "~ (tilde)"
    SendKeys "%{left}{DOWN}n~"

    SendKeys "%x{DOWN}~"
    SendKeys "{NUMLOCK}", True
    SendKeys "%fx"
    SendKeys "n"
End Sub

The last lines represent Alt-File-Exit, then the msgbox pops up, n is supposed to represent "no" and the program should close, it does not recognize the "n" for "no"

enter image description here

If send keys will not work, would anybody know how to close sketchup from excel?


Solution

  • This should work:

    Shell "taskkill /F /IM sketchup.exe", vbNormalFocus

    As shown here:

    TaskKill: Kill processes from the command line (CMD)

    and the second half of this answer:

    Execute a command in command prompt using excel VBA