获取Outlook.PropertyPageSite,以便您可以调用OnStatusChange()并更新UI

时间:2014-01-14 21:03:31

标签: .net vb.net outlook vsto outlook-addin

outlook PropertyPage界面需要只读布尔属性Dirty

当此项设置为true时,属性页选项对话框中的“应用”按钮将变为启用状态。

Apply

根据此walkthrough,您必须致电OnStatusChange以通知用户界面Dirty的值已更改。

据称,可以通过以下方式获取:

Dim ppSite As Outlook.PropertyPageSite = Parent
ppSite.OnStatusChange()

Parent总是不返回任何内容,所以当我更新脏标志时,我没有机制告诉用户界面。

我该怎么做?


我正在使用此discussion中生成的基本步骤来设置选项页面 这是我的代码的完整实现:​​

添加了名为SendReminderOptions的新用户控件:

<ComVisible(True)>
Public Class SendReminderOptions : Inherits UserControl : Implements Outlook.PropertyPage

    Const captionDispID As Integer = -518

    Private _dirty As Boolean = False
    Public ReadOnly Property Dirty As Boolean Implements Microsoft.Office.Interop.Outlook.PropertyPage.Dirty
        Get
            Return _dirty
        End Get
    End Property

    Public Sub SetDirty(newValue As Boolean)
        _dirty = newValue
        Dim ppSite As Outlook.PropertyPageSite = Parent
        ppSite.OnStatusChange()
    End Sub

    <DispId(captionDispID)> _
    Public ReadOnly Property PageCaption() As String
        Get
            Return "Send Reminder Options"
        End Get
    End Property

    Public Sub GetPageInfo(ByRef HelpFile As String, ByRef HelpContext As Integer) Implements Microsoft.Office.Interop.Outlook.PropertyPage.GetPageInfo

    End Sub

    Public Sub Apply() Implements Microsoft.Office.Interop.Outlook.PropertyPage.Apply

    End Sub

End Class

将以下代码添加到ThisAddIn

Private Sub ThisAddIn_Startup() Handles Me.Startup
    Dim myOutlook As Outlook.Application = Globals.ThisAddIn.Application
    AddHandler myOutlook.OptionsPagesAdd, AddressOf AddOptionsPage
End Sub

Private Sub AddOptionsPage(ByVal pages As PropertyPages)
    pages.Add(New SendReminderOptions(), "Options")
End Sub

1 个答案:

答案 0 :(得分:2)

此函数使用Reflection获取父PropertyPageSite对象。必须在Load事件中调用它。

Visual Basic

Private Function GetPropertyPageSite() As Outlook.PropertyPageSite
    Dim objType As Type = GetType(System.Object)
    Dim assemblyPath As String = objType.Assembly.CodeBase.Replace("mscorlib.dll", "System.Windows.Forms.dll").Replace("file:///", "")
    Dim assemblyName As String = System.Reflection.AssemblyName.GetAssemblyName(assemblyPath).FullName

    Dim unsafeNativeMethods As Type = Type.[GetType](System.Reflection.Assembly.CreateQualifiedName(assemblyName, "System.Windows.Forms.UnsafeNativeMethods"))
    Dim oleObjectType As Type = unsafeNativeMethods.GetNestedType("IOleObject")

    Dim methodInfo As System.Reflection.MethodInfo = oleObjectType.GetMethod("GetClientSite")
    Dim propertyPageSite As Object = methodInfo.Invoke(Me, Nothing)

    Return DirectCast(propertyPageSite, Outlook.PropertyPageSite)
End Function

<强> CSHARP

private Outlook.PropertyPageSite GetPropertyPageSite()
{
    Type objType  = typeof(System.Object);
    string assemblyPath = objType.Assembly.CodeBase.Replace("mscorlib.dll", "System.Windows.Forms.dll").Replace("file:///", "");
    string assemblyName = System.Reflection.AssemblyName.GetAssemblyName(assemblyPath).FullName;

    Type unsafeNativeMethods = Type.GetType(System.Reflection.Assembly.CreateQualifiedName(assemblyName, "System.Windows.Forms.UnsafeNativeMethods"));
    Type oleObjectType = unsafeNativeMethods.GetNestedType("IOleObject");

    System.Reflection.MethodInfo methodInfo = oleObjectType.GetMethod("GetClientSite");
    Object propertyPageSite = methodInfo.Invoke(this, null);

    return (Outlook.PropertyPageSite)propertyPageSite;
}

来自How to implement OL PropertyPage / Customize Outlook Options Dialog