从Excel关闭草图

时间:2018-10-29 17:23:15

标签: excel-vba sketchup

我要从excel打开Sketchup,然后运行一个插件,我使用下面的代码打开Sketchup,然后使用SendKeys选择插件。

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”代表“ no”

enter image description here

如果发送键不起作用,有人会知道如何从Excel中关闭Sketchup吗?

3 个答案:

答案 0 :(得分:0)

自SketchUp 2015起,SketchUp Ruby API中有一个Model#close模型。如果使用true作为参数调用,它将忽略更改。它不会关闭应用程序,但会带您进入一种状态,可以关闭应用程序而无需担心保存显示内容。

答案 1 :(得分:0)

这应该有效:

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

如此处所示:

  

TaskKill: Kill processes from the command line (CMD)

以及此答案的后半部分:

  

Execute a command in command prompt using excel VBA

答案 2 :(得分:0)

在Excel中启动流程时,您可以从OpenProcess获取启动的 processID 。您也可以导入TerminateProcess并使用从OpenProcess获得的 processID 关闭(终止)它。