如何在Outlook VBA中的自定义窗体属性上设置事件

时间:2019-01-20 09:18:18

标签: vba forms events outlook outlook-vba

在我的自定义约会表单中,我有一个修改后的表单页面(P.2),当第一个组合框(项目名称)中的值发生更改时,第二个组合框(项目活动)需要重新填充。

我想通过在第一个组合框上设置一个Change-event来实现这一点。

但是,我无法使它正常工作。

有人知道我搞砸了吗?还是有更好的选择?预先感谢。

我尝试了两种方法:

1。 ThisOutlookSession中的CustomPropertyChange事件处理程序 根据{{​​3}},我应用了CustomPropertyChange事件处理程序:

Private WithEvents objInspectors As Outlook.Inspectors
Private WithEvents objAppointment As Outlook.AppointmentItem
Private Sub Application_Startup()
    Set objInspectors = Application.Inspectors
End Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class = olAppointment Then
       Set objAppointment = Inspector.CurrentItem
    End If
End Sub
Private Sub objAppointment_Open(Cancel As Boolean)
' This procedure triggers normally
    With objAppointment
          .Recipients.ResolveAll
          .GetInspector.SetCurrentFormPage "P.2"
    End With
End Sub
Private Sub objAppointment_CustomPropertyChange(ByVal myPropName)
' After adding this procedure, the error (see below) appears
    MsgBox myPropName
End Sub

现在,当我启动Outlook时,出现以下错误,并指向“ Private Sub Private Sub objAppointment_CustomPropertyChange”行:

This returns the error “Compile error: Procedure declaration does not match description of event or procedure having the same name”. 

但是,事件objAppointment_Open起作用并且符合同一手册。

2。自定义表单脚本 我尝试的第二种方法是通过自定义表单脚本(约会项>“开发人员”选项卡>“设计此表单”>“查看代码”):

Sub Item_CustomPropertyChange(ByVal Name)
    MsgBox "The " & Name & " custom property changed."
End Sub

没有错误,但是也没有出现任何消​​息框。经过研究,我发现自定义表单脚本默认情况下处于禁用状态。我根据this manual启用了它。 此后,仍无反应。 除此之外,当我在自定义脚本中添加另一个事件处理程序(例如Item_Open),然后关闭并重新打开de editor时,这是我所看到的:

Sub Item_CustomPropertyChange(ByVal Name)
    MsgBox "The " & Name & " custom property changed."
End Sub ??@___??
___?_????????____??
___?_??????__??????????`_????????___

1 个答案:

答案 0 :(得分:1)

我已经通过解决上述第一种方法(ThisOutlookSession中的CustomPropertyChange事件处理程序)解决了这个问题。由Inspector.CurrentItem生成的AppointmentItem对象为只读(source)。我高度怀疑这是问题所在,因为事实证明打开和读取事件确实起作用,而写入和CustomPropertyChange事件却没有。

我现在将代码更改为:

Public WithEvents myItem As Outlook.AppointmentItem
Private Sub Application_ItemLoad(ByVal Item As Object)
    ' Confirm if it actually is an appointment:
    If Item.Class <> olAppointment Then
        Exit Sub
    End If

    ' Initiate event handlers
    Set myItem = Item
End Sub
Private Sub myItem_Open(Cancel As Boolean)
    Debug.Print "myItem_Open triggered"
End Sub
Private Sub myItem_CustomPropertyChange(ByVal Name As String)
    Select Case Name
        Case "cfProjectName" 'Field name of custom property
            Debug.Print "Case cfProjectName"
            ' Performing actions based on first combobox change here
        Case "cfCustomActivityName" 'Field name of custom property
            Debug.Print "Case cfCustomActivityName"
            ' Performing actions based on second combobox change here
        Case Else
    End Select
End Sub

我仍然不知道第二种方法(自定义表单脚本)出了什么问题。但是,这已经不是首选方法,因为我必须进行注册表更改。