如何以编程方式设置outlook文件夹初始视图

时间:2013-03-13 09:29:58

标签: outlook

我想设置一个公共文件夹以获得初始视图(自定义视图),我知道如何在Outlook 2007上手动执行此操作,但是,我找不到可以在Interop中使用的任何属性或方法(文件夹和MAPI)文件夹)可以做到这一点。

经过几个小时的谷歌搜索后,我发现了以下内容:

Imports NUnit.Framework
Imports System.Windows.Forms
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Net

Imports System.Runtime.InteropServices
Imports Outlook = Microsoft.Office.Interop.Outlook

<TestFixture()>
Public Class TestOutlook
    <Explicit()>
    <Test()>
    Public Sub TestSetFolderInitialView()
        Dim ol As New Outlook.Application
        Dim exCatched As Exception = Nothing
        Try
            ' Get My Mailbox 
            Dim myFolder As Outlook.MAPIFolder = Nothing
            For i As Integer = 0 To ol.Session.Folders.Count - 1
                myFolder = ol.Session.Folders(i + 1)
                If myFolder.Name = "Mailbox - Rex" Then ' Change it to your mail box name
                    Exit For
                End If
            Next

            ' Get the folder I want to Set initial view
            Dim testFolder As Outlook.MAPIFolder = Nothing
            If myFolder IsNot Nothing Then
                For i As Integer = 0 To myFolder.Folders.Count - 1
                    Dim pFolder = myFolder.Folders(i + 1)
                    If pFolder.Name = "Inbox" Then
                        For Each fol As Outlook.MAPIFolder In pFolder.Folders
                            If fol.Name = "TestFolder" Then
                                testFolder = fol
                                Exit For
                            End If
                        Next
                        Exit For
                    End If
                Next
            End If

            If testFolder IsNot Nothing Then
                Try
                    ' Create a test view
                    Dim newVw = CType(testFolder.Views.Add("RexTest-" & DateTime.Now.ToString("yyyyMMdd-hhmmss"),
                                                                   Outlook.OlViewType.olTableView,
                                                                   Outlook.OlViewSaveOption.olViewSaveOptionThisFolderEveryone), 
                                             Outlook.TableView)
                    newVw.LockUserChanges = True
                    newVw.Save()
                    newVw.Apply()

                    ' PR_DEFAULT_VIEW_ENTRYID:
                    Dim ns = "http://schemas.microsoft.com/mapi/proptag/"
                    Dim PR_DEFAULT_VIEW_ENTRYID = "0x36160102"
                    Dim PR_FOLDER_XVIEWINFO_E = "0x36E00102"
                    Dim defaultVw = testFolder.PropertyAccessor.GetProperty(ns & PR_DEFAULT_VIEW_ENTRYID)
                    Dim xVwInfo = testFolder.PropertyAccessor.GetProperty(ns & PR_FOLDER_XVIEWINFO_E)

                    ' the defaultVw is nothing for the first time (actually throw exception)
                    ' if i manually change it from the outlook, 
                    '   the value will be something like: 000000004B593F3D35EF8C42AB181C105AE444D40700E46C905CB9ABE446AA44351902AFC40E000026BF7A8C000040DB82FE9B98724F9B222A9C9BDB42CD0000005CF0280000

                    ' **** The problem is how to get the correct binary data for the newly created view so i can set it like this: *****
                    'testFolder.PropertyAccessor.SetProperty(ns & PR_DEFAULT_VIEW_ENTRYID, testFolder.PropertyAccessor.StringToBinary(newVw.Name))
                Catch ex As Exception
                    ' _log.Warn(String.Format("Error set initial view {0} to folder - {1}", newVw.Name, testFolder.Name), ex)
                    exCatched = ex
                    ' First time error 'The property "http://schemas.microsoft.com/mapi/proptag/0x36160102" is unknown or cannot be found' will be shown
                    ' If we set the initial view of the folder in the outlook, this error will go away
                End Try
            End If
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
            Debug.WriteLine(ex.StackTrace)
            exCatched = ex
        Finally
            If ol IsNot Nothing Then
                Marshal.ReleaseComObject(ol)
                ol = Nothing
            End If
        End Try

        If exCatched IsNot Nothing Then
            Throw exCatched
        End If
    End Sub
End Class

现在唯一的左边部分是如何从新创建的视图中获取正确的二进制数据。一些操纵二进制值的线索:http://microsoft.public.win32.programmer.messaging.narkive.com/x1fNHHA5/default-view

然而它是用不同的语言编写的,我不知道如何在vb或c#中创建它。

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您是否尝试过MAPIFolder.CurrentView和MAPIFolder.Views?