从 Excel 关闭草图

Close sketchup from Excel

我从excel打开sketchup,然后运行一个插件,我用下面的代码打开sketchup,然后使用SendKeys到select插件。

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

最后一行代表Alt-File-Exit,然后弹出msgbox,n应该代表"no",程序应该关闭,它不识别"n" for "no"

如果发送键不起作用,有人知道如何从 excel 关闭 sketchup 吗?

自 SketchUp 2015 以来,SketchUp Ruby API 中有一个 Model#close 模型。如果以 true 作为参数调用,它会忽略更改。它不会关闭应用程序,但它会让您进入可以关闭应用程序的状态,而不会出现关于保存显示的任何问题。

这应该有效:

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

如图所示:

TaskKill: Kill processes from the command line (CMD)

以及此答案的后半部分:

Execute a command in command prompt using excel VBA

当您从 Excel 中启动进程时,您可以获得从 OpenProcess 返回的 processID。您还可以导入 TerminateProcess 并使用从 OpenProcess 获得的 processID 关闭(终止)它。