Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set outlook folder initial view programatically
    text
    copied!<p>I want to set a public folder to have an initial view (custom view), I know how to do it manually on outlook 2007, however, I cannot find any property or method i can use in the Interop (Folder and MAPI folder) that can do this. </p> <p>After a few hours of googling, i came out the following:</p> <pre><code>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 &lt;TestFixture()&gt; Public Class TestOutlook &lt;Explicit()&gt; &lt;Test()&gt; 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-" &amp; 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 &amp; PR_DEFAULT_VIEW_ENTRYID) Dim xVwInfo = testFolder.PropertyAccessor.GetProperty(ns &amp; 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 &amp; 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 </code></pre> <p>Now the only left part is how to get the correct binary data from the newly created view. some clue to manipulate the binary value: <a href="http://microsoft.public.win32.programmer.messaging.narkive.com/x1fNHHA5/default-view" rel="nofollow">http://microsoft.public.win32.programmer.messaging.narkive.com/x1fNHHA5/default-view</a></p> <p>however it was written in a different language and I got no idea how to make it in vb or c#. </p> <p>any help appreciated. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload