NavigationFolders.add() crashes Outlook for shared calendars? - vba

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.

Related

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.

VBA Outlook loop through my colleagues tasks

I found how to assign tasks to my colleagues.
Is it possible to loop through my colleagues tasks with filter on project name, so I could monitorate tasks status?
In a team project we assign tasks to each other, and the project manager could in one click see important tasks, their status, due date...
You can use the GetSharedDefaultFolder method of the Namespace class to get a Folder object that represents the specified default folder for the specified user. For example, to get the shared tasks folder you can use the following code:
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 ShowTasks(myNamespace, myRecipient)
End If
End Sub
Sub ShowTasks(myNamespace, myRecipient)
Dim TaskFolder As Outlook.Folder
Set TaskFolder = _
myNamespace.GetSharedDefaultFolder _
(myRecipient, olFolderTasks)
TaskFolder.Display
End Sub

outlook shared mailbox contacts in a excel list with VBA

I'm looking for a vba code in excel, he should contacts from a shared mailbox (different exchange account called shared mailbox in outlook) transfer to a excel list, with the normal contacts from outlook it works I used this:
Set nsOutlook = applOutlook.GetNamespace("MAPI")
Set cfOutlook = nsOutlook.GetDefaultFolder(olFolderContacts)
with Set olcontacts = cfOutlook.Folders("name")
I have used the "name" where is displayed in outlook
it not works he not find the folder.
is there a solution without recipient?
because there will be several user-all with the same shared mailbox
I hope you can help me.
The Outlook object model provides the GetSharedDefaultFolder method of the Namespace class which returns a Folder object that represents the specified default folder for the specified user. For example:
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

How to automate saving an inbox message to a folder when closing after reading - Outlook 2010 VBA

I want to keep this in VBA. I'm seeking info on how to work around the following issue.
I get this error:
The item's properties and methods cannot be used inside this event procedure. MS has stopped people being able to use the .Close, .Move and .Delete methods in the Inspector_Close event.
I've seen suggestions to use threading to run a delayed macro, but can't find help on this, and suspect it may not be available in VBA.
My code is as follows:
Private Sub objInspector_Close()
Dim objNS As NameSpace
Dim objFolder As MAPIFolder
'On Error Resume Next
Set objNS = Application.Session
If Not mailSent Then
If objInspector.CurrentItem.Class = olMail Then
'Mail inspector is closing
If objInspector.CurrentItem.Parent = "Inbox" Then
Set objFolder = objNS.PickFolder
If Not objFolder Is Nothing And IsInDefaultStore(objFolder) _
And objFolder.DefaultItemType = olMailItem Then
Set objInspector.CurrentItem.Move = objFolder
End If
End If
End If
Else
mailSent = False
End If
Set objFolder = Nothing
Set objNS = Nothing
End Sub
The global mailSent Boolean is there to prevent this event code executing when I send / close an email.
The error occurs on Set objInspector.CurrentItem.Move = objFolder.
Is there a way for me to delay this until the event ends or perhaps to set some flags on the email item and then run a macro over all emails in my inbox later to move them to folders.
I work on multiple projects and maintain multiple email folders and am looking for ways to automate my email management. I've seen other pages where folder names are derived from email subjects but I don't want to do that.
Thanks for your help.
You may consider adding a user property which can mark the message for moving etc. Then you can use the Find/FindNext or Restrict methods for searching marked items. You can read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
Also you can use the GetTable method of the Folder class which obtains a Table object that contains items filtered by Filter.
As you probably know the Outlook object model can't be used from another threads. You need to use a low-level API - Extended MAPI which supports secondary threads. Or any other third-party wrappers around that API, for example - Redemption.
You could abandon the idea of using a trigger and move "manually"
Option Explicit
Private Sub MoveCurrentItem()
Dim objNS As Namespace
Dim objFolder As folder
Dim currItem As Object
Dim uPrompt As String
Set objNS = Application.Session
On Error Resume Next
Set currItem = ActiveInspector.currentItem
On Error GoTo 0
If currItem Is Nothing Then GoTo ExitRoutine
If currItem.Class = olMail Then
If currItem.Sent Then ' reading not composing
If currItem.Parent = "Inbox" Then
Set objFolder = objNS.PickFolder
If Not objFolder Is Nothing And IsInDefaultStore(objFolder) _
And objFolder.DefaultItemType = olMailItem Then
currItem.Move objFolder
End If
End If
End If
End If
ExitRoutine:
Set currItem = Nothing
Set objFolder = Nothing
Set objNS = Nothing
End Sub