【发布时间】:2013-04-03 13:08:46
【问题描述】:
我需要在 Excel VBA 中编写一个宏,它会在关闭 excel 后终止在 windows 任务中运行的进程。我在事件 workbook_BeforeClose 上尝试过这样做
Private Sub Workbook_BeforeClose(CANCEL As Boolean)
Run "MacroCloseProcess"
End Sub
MacroCloseProcess 是这样定义的
Private Sub MacroCloseProcess()
Dim oWMT As Object, oProcess As Object
Set oWMT = GetObject("winmgmts://")
For Each oProcess In oWMT.InstancesOf("Win32_Process")
If (oProcess.name) = pWcfHostApp Then
If oProcess.Terminate() = 0 Then Exit Sub
End If
Next
End Sub
这可行,但是,如果工作簿中有更改,excel 会为用户提供以下选项 "是否要保存对 'Sheet1.xlsx' 所做的更改?保存、不保存、取消
如果用户单击取消,Excel 不会退出(根据设计),但是哦,该过程已终止,因为它处于“BeforeClose”事件中。我怎样才能编写这段代码,以便它在 excel 关闭后命中?
【问题讨论】: