VBA selecting right mailbox? - vba

In my Outlook, I have two accounts configured:
my main account / inbox: chip#mail.com
a shared mailbox: shared#mail.com
So there are two different mailboxes.
I could hide some default folders using VBA in both mailboxes.
But now I want to unhide a default folder in the shared mailbox shared#mail.com.
This is the suggested code:
Option Explicit
Public Sub UnHideFolders()
Dim oFolder As Outlook.Folder
Dim oPA As Outlook.propertyAccessor
Dim PropName, Value, FolderType As String
PropName = "http://schemas.microsoft.com/mapi/proptag/0x10F4000B"
Value = False
Set oFolder = Session.GetDefaultFolder(olFolderNotes)
Set oPA = oFolder.propertyAccessor
oPA.SetProperty PropName, Value
Set oFolder = Nothing
Set oPA = Nothing
End Sub
This works fine for the first mailbox account / inbox chip#mail.com, but I can't get it to work for the second account / shared mailbox shared#mail.com.
How do I have to change the above code to unhide folders in the second account / shared mailbox?
Thanks in advance for your support!
A somehow desperate Chipy

The following code can be used for getting the folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Notes folder for the user who is currently logged on:
Set oFolder = Session.GetDefaultFolder(olFolderNotes)
To get folders from a shared account you need to use the NameSpace.GetSharedDefaultFolder method which returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder). The following code illustrates a possible usage of the method to get a shared calendar folder:
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("shared#mail.com")
myRecipient.Resolve
If myRecipient.Resolved Then
Call ShowCalendar(myNamespace, myRecipient)
End If
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.Folder
Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub
Note, the NameSpace.CreateRecipient method accepts the name of the recipient - it can be a string representing the display name, the alias, or the full SMTP email address of the recipient.
If that is not a standard folder or visible in Outlook you may consider using the Store.GetDefaultFolder method which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

To get the folder, try
Set oFolder = Session.Accounts("shared#mail.com").DeliveryStore.GetDefaultFolder(olFolderNotes)
... then re-use your existing code to manage the folder.

Related

how to read outlook email in a directory files in vb.net

I am new in vb.net development and I must read informations (subject, body, ...) in Outlook email files that are in a disk directory (D:\mails\to-read\message1.msg, D:\mails\to-read\message2.msg, ...).
Is it possible ?
Can you please explain me ? With example ?
Thanks for your help.
If the Outlook object model (Outlook automation) is a possible option you can use the NameSpace.OpenSharedItem method which opens a shared item from a specified path or URL. This method is used to open iCalendar appointment (.ics) files, vCard (.vcf) files, and Outlook message (.msg) files. So, in the code you will get a MailItem object where you could get all the required properties.
Public Sub TestOpenSharedItem()
Dim oNamespace As Outlook.NameSpace
Dim oSharedItem As Outlook.MailItem
Dim oFolder As Outlook.Folder
' Get a reference to a NameSpace object.
Set oNamespace = Application.GetNamespace("MAPI")'Open the Signed Message (.msg) file containing the shared item.
Set oSharedItem = oNamespace.OpenSharedItem("C:\Temp\RegularMessage.msg")
MsgBox oSharedItem.Subject
oSharedItem.Close (olDiscard)
Set oSharedItem = Nothing
Set oSharedItem = Nothing
Set oFSO = Nothing
Set oNamespace = Nothing
End Sub

Selecting inbox when multiple exist [duplicate]

This question already has answers here:
Get reference to additional Inbox
(3 answers)
Closed 1 year ago.
Hi I am using the following macro to create a batch of new folders in an inbox. It performs fantastically however I can't for the life of me figure out how to select a different inbox (inbox1, inbox2, inbox3) all different email accounts.
code is here: http://www.slipstick.com/macros/Create%20subfolders%20at%20multiple%20levels.txt
Instead of using Session.GetDefaultFolder, call Session.CreateRecipient / Recipient.Resolve / Session.GetSharedDefaultFolder
If all these inboxes are configured in Outlook you can use the Stores collection to iterate over stores and using the Store.GetDefaultFolder method which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument.
If you need to access shared mailboxes you need to use the NameSpace.GetSharedDefaultFolder method which returns a Folder object that represents the specified default folder for the specified user.
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Dan Wilson")
myRecipient.Resolve
If myRecipient.Resolved Then
Call ShowCalendar(myNamespace, myRecipient)
End If
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.Folder
Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub

VSTO Outlook: Cannot find subfolders of shared inbox

I am trying to move mails from any folder to a very specific subfolder of a shared inbox. After trying many different things that did not work I tried to loop through every single folder in the inbox und check if it has the name I am looking for. When I try moving the mailitem, I get the message that the element could not be moved. After searching a little bit longer for the cause I found out, that apparently no folder inside of my inbox exist and the for each loop exits without checking a single entry. So how am I supposed to access a specific subfolder which i only know the name of?
Relevant code:
Private Const destFolder = "myfoldername"
Public Function MoveMail()
SelectedItems = Globals.ThisAddIn.Application.ActiveExplorer.Selection
For Each Item In SelectedItems
Call MoveSelectedMail(Item)
Next Item
End Function
Function MoveSelectedMail(Item As Outlook.MailItem)
Item.Move(GetFolderToMove(destFolder))
End Function
Function GetFolderToMove(ByVal FolderName As String) As Outlook.Folder
Dim NS As Outlook.NameSpace
Dim objOwner As Outlook.Recipient
NS = Globals.ThisAddIn.Application.GetNamespace("MAPI")
objOwner = NS.CreateRecipient("NameofSharedMailbox")
objOwner.Resolve()
If objOwner.Resolved Then
Dim inbox As Outlook.Folder
inbox = NS.GetSharedDefaultFolder(objOwner, OlDefaultFolders.olFolderInbox)
For Each folder As Outlook.Folder In inbox.Folders
MsgBox(folder.Name)
If folder.Name = FolderName Then
Return folder
End If
Next folder
End If
End Function
This is the code I used in VBA but did not work when I started trying to do the same thing as VSTO addin:
Function GetFolderToMove(ByVal FolderPath As String) As Outlook.Folder
Dim NS As Outlook.NameSpace
Dim objOwner As Outlook.Recipient
Set NS = Application.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("NameofSharedMailbox")
objOwner.Resolve
If objOwner.Resolved Then
Set GetFolderPath = NS.GetSharedDefaultFolder(objOwner, olFolderInbox).Folders(destFolder)
End If
End Function
What I tried but did not help me solve this problem:
Tried returning only the shared inbox and this worked, however, the inbox is not the folder I want to move the mails to.
In short: I am trying to move a mail to a subfolder of a shared inbox but there seem to be no subfolders according to the error messages.
Hoping you can help me.
Edit:
My Problem might be a little bit out of place as it seems that there might be a problem with the permissions my outlook account has. If the problem is going to be resolved that way, I will update this thread and close ist.
I solved it - I am not entirely sure how exactly this could have happened but it certainly had to do with the permissions I had.

Is it possible to select a folder based on it's name rather then on it's path? VBA - Outlook

Recently I am trying to create a function which selects specific folders.
I tested this at one user and it works. The problem however is that I also want to use this function at other users but not having to rewrite the function based on their folder structure.
The function works with a few folder names which all of them have (same names).
It consists of 1 main folder: #MemoScan and 4 sub-folders.
Based on these folder names I want to count how many mail items are in them.
I created the following function to do this:
Function HowManyEmails() As Integer
Dim objOutlook As Object, objnSpace As Object, MyCurrentFolder As MAPIFolder
Dim EmailCount As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
Set MyCurrentFolder = objnSpace.folders("William").folders("#MemoScan")
sFolder = MyCurrentFolder
For Each Folder In MyCurrentFolder.folders
sFolder = Folder
sSubmap = Right(sFolder, Len(sFolder) - 1)
For Each Item In Folder.Items
If TypeName(Item) = "MailItem" Then
EmailCount = EmailCount + 1
End If
Next Item
Next Folder
HowManyEmails = EmailCount
End Function
As you can see the folder that needs to be checked is hard-coded (needs to be since it runs on a close outlook event and nothing is selected).
The path now is: objnSpace.folders("William").folders("#MemoScan")
The thing is however that the main account/folder William wont be there at other users. My question is, how can I adjust it in such way that it will just look for the #MemoScan folder which is of the same at every user? Is this even possible?
If I leave the main William namespace out then it won't be able to find the #MemoScan folder.
The folder structure at this particular user is as follows:
The Namespace class provides the Stores property which returns a Stores collection object that represents all the Store objects in the current profile. The Store class provides the GetRootFolder method which returns a Folder object representing the root-level folder of the Store. You can use the GetRootFolder method to enumerate the subfolders of the root folder of the Store. Unlike NameSpace.Folders which contains all folders for all stores in the current profile, Store.GetRootFolder.Folders allows you to enumerate all folders for a given Store object in the current profile.
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
End Sub
Also you can run the code against the currently selected folder in Outlook. The CurrentFolder property of the Explorer class returns a Folder object that represents the current folder displayed in the explorer.

NavigationFolders.add() crashes Outlook for shared calendars?

Simple enough line here:
Set navFol = navGroup.NavigationFolders.Add(cal)
This works as expected for any local calendars, but it instantly crashes Outlook if "cal" is a shared calendar. Anyone know a workaround to move shared calendars around between navigation folders? I'm quite new to VBA, just hacking my way around to get a macro to do a simple something for me -- or at least something which really should be simple if not for this.
I doubt it matters, but just in case, "cal" is being set in a for loop by iterating through a list of EntryIDs like so:
Set cal = Application.GetNamespace("MAPI").GetFolderFromID(str)
And it's not the variable assignment that's failing there (which is why the above line should be irrelevant). I can do anything else with the calendar whether or not it's shared: read the name, grab appointments from it, etc. Outlook just apparently does not like using shared calendars as arguments for NavigationFolders.Add().
EDIT: I'm talking about NON-default calendars shared via sharing invitations. GetDefaultSharedFolder or the like isn't what I want.
Try to use the GetSharedDefaultFolder method of the Namespace class to get the shared folder instead.
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev")
myRecipient.Resolve
If myRecipient.Resolved Then
Call ShowCalendar(myNamespace, myRecipient)
End If
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.Folder
Set CalendarFolder = _
myNamespace.GetSharedDefaultFolder _
(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub
The Add method of the NavigationFolders class adds the specified Folder, as a NavigationFolder object, to the end of the NavigationFolders collection.