如何使用vb.net在我的Outlook中创建顶级文件夹 - VB.NET,Outlook 2013

时间:2015-06-16 07:55:05

标签: vb.net outlook outlook-addin

正如标题所说,我正试图在我的Outlook中创建一个顶级文件夹,但我没有取得任何成功。我已经阅读了几个教程和代码片段,但它们似乎并不成功。

所以现在我有这段代码在Inbox文件夹下创建一个文件夹:

Dim objFolder As Outlook.MAPIFolder
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()

objFolder.Folders.Add("Some folder", Outlook.OlDefaultFolders.olFolderInbox)

问题是,如何创建相同的文件夹,然后创建顶级文件夹,而不是收件箱文件夹的子文件夹。

我已经尝试过这样做:

objFolder.Folders.Add("Some folder")但这并没有奏效。

2 个答案:

答案 0 :(得分:2)

热门文件夹(导航窗格中的根节点)是商店。如果需要在配置文件中添加新商店,可以使用Namesapace类的AddStoreEx方法,该方法将指定格式的个人文件夹文件(.pst)添加到当前配置文件中。有关详细信息,请参阅How to: Add or Remove a Store

如果您需要创建顶级文件夹(与Inbox等标准文件夹处于同一级别),您可以获取收件箱的Parent文件夹对象或任何其他默认文件夹并添加那里有一个新文件夹。例如:

  Dim objFolder As Outlook.MAPIFolder
  Dim parentFolder as Outlook.MAPIFolder
  Dim objOutlook As Outlook._Application
  objOutlook = New Outlook.Application()

  myNamespace = objOutlook.GetNamespace("MAPI") 
  objFolder = myNamespace.GetDefaultFolder(olFolderInbox) 
  parentFolder = objFolder.Parent
  parentFolder.Folders.Add("Some folder", Outlook.OlDefaultFolders.olFolderInbox)

此外,您可能会发现Store类的GetRootFolder方法很有帮助。它返回一个Folder对象,表示Store的根级文件夹。您可以使用GetRootFolder方法枚举Store的根文件夹的子文件夹。与包含当前配置文件中所有商店的所有文件夹的NameSpace.Folders不同,Store.GetRootFolder.Folders允许您枚举当前配置文件中给定Store对象的所有文件夹。

 Sub EnumerateFoldersInStores() 
  Dim colStores As Outlook.Stores 
  Dim oStore As Outlook.Store 
  Dim oRoot As Outlook.Folder 
  On Error Resume Next 
  Set colStores = Application.Session.Stores 
  For Each oStore In colStores 
   Set oRoot = oStore.GetRootFolder 
   Debug.Print (oRoot.FolderPath) 
   EnumerateFolders oRoot 
  Next
 End Sub 

 Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder) 
  Dim folders As Outlook.folders 
  Dim Folder As Outlook.Folder 
  Dim foldercount As Integer 
  On Error Resume Next 
  Set folders = oFolder.folders 
  foldercount = folders.Count 
  'Check if there are any folders below oFolder 
  If foldercount Then 
  For Each Folder In folders 
   Debug.Print (Folder.FolderPath) 
   EnumerateFolders Folder 
  Next 
 End If

答案 1 :(得分:0)

Private Sub CreateNewFolder()
        Dim oApp As Outlook.Application = New Outlook.Application
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
        Dim InboxFolder As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim customFolder As Outlook.MAPIFolder
        Try
            customFolder = InboxFolder.Folders.Add("Vellaichamy", Outlook _
               .OlDefaultFolders.olFolderInbox)
            InboxFolder.Folders("Authorcode").Display()
        Catch ex As Exception
            MessageBox.Show("The following error occurred: " & ex.Message)
        End Try
    End Sub