用于从其他应用程序切换到Excel的事件处理程序?

时间:2013-08-19 18:44:39

标签: excel vba

我想在从其他应用程序切换时激活工作簿。我正在使用Excel 2010。

在ThisWorkbook对象中,我尝试了以下内容:

Private Sub Workbook_Activate()
    MsgBox "1"
End Sub

Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    MsgBox "2"
End Sub

在课程模块中,我尝试过这些:

Public WithEvents appevent As Application
Private Sub appevent_ProtectedViewWindowActivate(ByVal Pvw As ProtectedViewWindow)
    MsgBox "1"
End Sub

Private Sub appevent_ProtectedViewWindowOpen(ByVal Pvw As ProtectedViewWindow)
    MsgBox "2"
End Sub

Private Sub appevent_WindowActivate(ByVal Wb As Workbook, ByVal Wn As Window)
    MsgBox "3"
End Sub

Private Sub appevent_WorkbookActivate(ByVal Wb As Workbook)
    MsgBox "4"
End Sub

Private Sub appevent_WorkbookDeactivate(ByVal Wb As Workbook)
    MsgBox "5"
End Sub

要求在激活此工作簿(单击或alt-tabbed-to)时禁用CellDragAndDrop属性,并在此工作簿未处于活动状态时重新启用它。

3 个答案:

答案 0 :(得分:1)

好的,我认为这首先是Ribbon自定义的工作。我无法使用功能区(不是说它不可能,但我没有看到任何会影响此功能的命令)。

你的类模块是这样的(我没有尝试其他视图说明你已枚举)。此模块封装了事件类,并包含应用程序级事件处理程序。为此,我认为您可能只需要WorkbookActivate。引发事件的工作簿将决定是否启用该属性。

Public WithEvents appevent As Application
Dim ret As String
Private Sub appevent_WorkbookActivate(ByVal wb As Workbook)

    Call ToggleDragAndDrop(wb, ret)
    'Comment out this line when satisfied it is working as expected
    MsgBox "Cell drag & drop enabled = " & ret
End Sub

在名为mod_DragDrop的标准模块中使用以下内容:

Option Explicit
Public XLEvents As New cEventClass
Sub SetEventHandler()

If XLEvents.appevent Is Nothing Then
    Set XLEvents.appevent = Application
End If

End Sub

Sub ToggleDragAndDrop(wb As Workbook, Optional ret$)

    Application.CellDragAndDrop = (wb.Name <> ThisWorkbook.Name)
    ret = Application.CellDragAndDrop
End Sub

将它放在Workbook_Open事件处理程序中:

Option Explicit
Private Sub Workbook_Open()
    'Create the event handler when the workbook opens
    Call mod_DragDrop.SetEventHandler
    Call mod_DragDrop.ToggleDragAndDrop(Me)

End Sub

注意:如果你&#34;结束&#34;运行时或在调试时做任何会导致状态丢失的事情,你将失去事件处理程序。这可以通过调用Workbook_Open过程来恢复,因此另外一个安全措施可能是在ThisWorkbook代码模块中添加它:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
' Additional safeguard in case state loss has killed the event handler:
' use some workbook-level events to re-instantiate the event handler

    Call Workbook_Open
End Sub

我已在我的Google Docs上提供了我的文件副本,以防上面提供的代码中存在错误的拼写错误。

答案 1 :(得分:1)

它位于选项->“启用填充手柄和单元格拖放”的“高级”选项卡下的一个选项中。

这不是VBA,但确实可以满足您的要求。

答案 2 :(得分:0)

我想四年后您仍然不会想到这个问题,所以我只想将您的评论转换为完整的答案,以便其他人更轻松地回答。该解决方案还可以在Excel 2016中使用。

Private Sub Workbook_Open()
    'MsgBox "Opened and disabled"
    Application.CellDragAndDrop = False
End Sub

Private Sub Workbook_WindowActivate(ByVal Wn As Excel.Window)
    'MsgBox "Activated and disabled"
    Application.CellDragAndDrop = False
End Sub

Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)
    'MsgBox "Deactivated and enabled"
    Application.CellDragAndDrop = True
End Sub

Private Sub Workbook_Before_Close(Cancel As Boolean)
    'MsgBox "Closed and enabled"
    Application.CellDragAndDrop = True
End Sub

我将此答案发布为社区Wiki,因为您实际上应该得到荣誉。