【问题标题】:Write a Macro in VBA - excel AFTER the user has closed the Excel在 VBA 中编写宏 - 用户关闭 Excel 后的 Excel
【发布时间】: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 关闭后命中?

【问题讨论】:

    标签: vba excel ms-office


    【解决方案1】:

    控制用户的决定。这是简单的代码,如果需要可以改进。

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    'take control of what user can do:
    If MsgBox("Do you want to save and exit?", vbYesNo) = vbYes Then
        Application.DisplayAlerts = False
            ThisWorkbook.Save
            'call your MacroCloseProcess here
        Application.DisplayAlerts = True
    Else
        Cancel = True
    End If
    End Sub
    

    *EDIT * 更好更优雅的选择:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    'take control of what user can do:
        Application.DisplayAlerts = False
        Dim filePath As Variant
    
        If Len(ThisWorkbook.Path) > 0 Then
            'has been already saved therefore just ask
            'this would be rarely meet, only whan call for the first time
            'or if this solution was placed in class module for all doc
    
            If MsgBox("Do you want to save and exit?", vbYesNo) = vbYes Then
                ThisWorkbook.Save
                'call your MacroCloseProcess here
                MsgBox "exit" '<-- to remove, kept for tests
            Else
                Cancel = True
            End If
        Else
            'document was not saved before- show standard file dialog
    
            filePath = Application.GetSaveAsFilename()
            If VarType(filePath) = vbString Then
    
                ActiveWorkbook.SaveAs Filename:=filePath
                'call your MacroCloseProcess here
                MsgBox "exit" '<-- to remove, kept for tests
    
            Else
                Cancel = True
            End If
        End If
        Application.DisplayAlerts = True
    End Sub
    

    【讨论】:

      【解决方案2】:

      其他一些想法没问题,我同意先保存,但你应该事先征得他们的许可。这也允许用户先保存,因为它首先检查它是否已保存。

      您应该在终止之前调用用户来保存工作簿。例如

      Private Sub Workbook_BeforeClose(CANCEL As Boolean)
      
      If Not Me.Saved Then 
          Msg = "Do you want to save the changes you made to " 
          Msg = Msg & Me.Name & "?" 
          Ans = MsgBox(Msg, vbQuestion + vbYesNoCancel) 
          Select Case Ans
              Case vbYes 
                  Me.Save 
              Case vbNo 
                  Me.Saved = True 
              Case vbCancel 
                  Cancel = True 
                  Exit Sub 
          End Select
      End If
      
      Run "MacroCloseProcess"
      
      End sub
      

      【讨论】:

        【解决方案3】:

        也许您可以在关闭 Excel 之前向用户提供您希望保存选项,然后通过

        为用户禁止任何进一步的对话框

        Application.DisplayAlerts=False

        ThisWorkbook.Close (False)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-10
          • 1970-01-01
          • 1970-01-01
          • 2018-01-16
          • 1970-01-01
          • 1970-01-01
          • 2011-01-14
          • 1970-01-01
          相关资源
          最近更新 更多