ArsedianIvan
ArsedianIvan

Reputation: 399

Outlook Selecting a Subfolder in the SharedMailbox using GetSharedDefaultFolder

I am having trouble trying to select a subfolder in SharedMailbox.
I have read a number of resources on GetSharedDefaultFolder.
However, struggling to put it together correctly.
Would be really great if you could help with this.

Sub ListOutlookEmailInfoInExcel()
  Dim olNS As Outlook.NameSpace
  Dim olTaskfolder As Outlook.MAPIFolder
  Dim olTask As Outlook.TaskItem
  Dim olItems As Outlook.Items

  Set o1NS = GetNamespace("MAPI")
  Set o1TaskFolder = o1NS.GetSharedDefaultFolder("Shared Folder 1", _
    olFolderInbox).Folders("admin")
  Set o1Items = o1TaskFolder.Items
End Sub

Upvotes: 2

Views: 10779

Answers (3)

in order to answer ArsedianIvan question, i figured out that with a sub name like "ListOutlookEmailInfoInExcel", he would be interested to save in an excel file some email information from a shared mailbox.

Here is my suggestion to do that :

Replace "Shared Folder 1" with your shared email name and I made this script stop after 80 email but you could modify or remove as you like

Sub ListEmailInfoInExcel()
Dim olNS As Outlook.Namespace
Dim olTaskfolder As Outlook.MAPIFolder
Dim olTask As Outlook.TaskItem
Dim olItems As Outlook.Items
Dim olMailItem As Outlook.MailItem
Dim objOwner As Outlook.Recipient
Dim i As Integer
Dim x As Integer
Dim Path As String
Dim RepTemp, NewRep As String
Set olNS = GetNamespace("MAPI")
Set objOwner = olNS.CreateRecipient("Shared Folder 1")
objOwner.Resolve

If objOwner.Resolved Then
    Set olTaskfolder = olNS.GetSharedDefaultFolder(objOwner, _
          olFolderInbox).Folders("Admin")
      
    Set olItems = olTaskfolder.Items
End If

'Print to a new excel file
Dim objExcel As Object
Dim sheet As Object
Set objExcel = CreateObject("Excel.Application")
Workbooks.Add

'Create a Temp rep in the C drive if it doesn't exist
RepTemp = "C:\Temp\"
NewRep = Dir(RepTemp, vbDirectory)
If NewRep = "" Then
      MkDir RepTemp
End If

For Each olMailItem In olTaskfolder.Items
i = i + 1
            Range("G" & i).Value = i
            Range("H" & i).Value = olMailItem.Sender
            Range("I" & i).Value = olMailItem.Subject
            Range("J" & i).Value = olMailItem.ReceivedTime
            
            ' it will print only 80 email details...
            If i = 80 Then
                GoTo isenough
            End If
Next
isenough:
Path = "C:\Temp\MyNewWorkbook"

ActiveWorkbook.SaveAs Filename:=Path
ActiveWorkbook.Close
End Sub

Yours,

Upvotes: 0

niton
niton

Reputation: 9199

You first resolve the owner as described here http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/

"You can use the mailbox owner's display name, alias, or email address when resolving the recipient."

I found email address always resolves so there is no benefit in verifying that it resolves.

Sub ListOutlookEmailInfoInExcel()

    Dim olNS As Outlook.NameSpace
    Dim olTaskfolder As Outlook.MAPIFolder
    Dim olTask As Outlook.TaskItem
    Dim olItems As Outlook.Items

    Dim objOwner As Outlook.Recipient

    Set olNS = GetNamespace("MAPI")

    Set objOwner = olNS.CreateRecipient("Shared Folder 1")
    objOwner.Resolve

    If objOwner.Resolved Then
        Set olTaskFolder = olNS.GetSharedDefaultFolder(objOwner, _
          olFolderInbox).Folders("admin")
        Set olItems = olTaskFolder.Items
    End If

End Sub

Upvotes: 2

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

The GetSharedDefaultFolder method of the Namespace class accepts two parameters: a Recipient object and the FolderType value.

The How to: Display a Shared Calendar of a Recipient article provides the following sample code in C#:

private void DisplayManagerCalendar()
{
    Outlook.AddressEntry addrEntry =
         Application.Session.CurrentUser.AddressEntry;
    if (addrEntry.Type == "EX")
    {
        Outlook.ExchangeUser manager =
        Application.Session.CurrentUser.
            AddressEntry.GetExchangeUser().GetExchangeUserManager();
        if (manager != null)
        {
            Outlook.Recipient recip =
                Application.Session.CreateRecipient(manager.Name);
            if (recip.Resolve())
            {
                try
                {
                    Outlook.Folder folder =
                       Application.Session.GetSharedDefaultFolder(
                          recip, Outlook.OlDefaultFolders.olFolderCalendar)
                       as Outlook.Folder;
                    folder.Display();
                }
                catch
                {
                    MessageBox.Show("Could not open manager's calendar.",
                       "GetSharedDefaultFolder Example",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                }
            }
        }
    }
}

Upvotes: 0

Related Questions