0

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?

braX
  • 11,506
  • 5
  • 20
  • 33
Davesexcel
  • 6,896
  • 2
  • 27
  • 42

3 Answers3

2

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

Poyda
  • 84
  • 11
0

Since SketchUp 2015 there has a Model#close model in the SketchUp Ruby API. If called with true as parameter it ignores the changes. It doesn't close the application, but it gets you to a state where you can close the application without any question about saving showing.

Eneroth3
  • 919
  • 8
  • 13
  • Thanks for the reply, I wanted to close the application. – Davesexcel May 10 '19 at 15:22
  • I think your existing method would sending keys would work if you first use this approach to discard model changes. Btw, what model changes is it you want to discard? If you just read the files without changing them, SketchUp shouldn't prompt you to save. – Eneroth3 May 11 '19 at 06:29
  • What is happening, excel is opening sketchup, then using send keys to move to the extensions menu and selecting a plugin to run, the plugin creates a drawing based on the info on the excel workbook and places the image onto the excel worksheet. Then excel uses send keys to close sketchup. – Davesexcel May 11 '19 at 15:07
  • If the changes the extension does to the model are wrapped inside an [operation](https://ruby.sketchup.com/Sketchup/Model.html#start_operation-instance_method), you can also call Sketchup.undo after the export to reset the model to its unmodified state. After doing this your Alt-File-Exit key presses should be able to exit SketchUp without any questions. – Eneroth3 May 11 '19 at 18:18
0

As you are starting the process from within Excel you can get the processID that you launched returned from OpenProcess. You can also Import TerminateProcess and using the processID you got from OpenProcess close (Terminate) it.

Paul Farry
  • 4,730
  • 2
  • 35
  • 61