[OL] Process all items in folder selected from context menu

Add-in Express™ Support Service
That's what is more important than anything else

[OL] Process all items in folder selected from context menu
 
Ole Andreas Ringdal


Rather green-ish


Posts: 46
Joined: 2006-05-30
I'm trying to add an item to the context menu when right clicking a folder in the Outlook folder tree, and then have it process all the items in the folder being right clicked. And not being forced to change the selection. For example, I would like to be able to stand in the Inbox folder, right click the Sent Items folder, and then have my context menu item for the Sent Items folder process all the items in the Sent Items folder without my Inbox losing focus.

I've managed to get the context menu right, and it displays the new items correctly. By using adxOutlookEvents_FolderContextMenuDisplay (since it has a target parameter), I also can get access to the folder name when the folder is right clicked.

What I don't know how to do is how to get to the folder when the context menu item is clicked and from there access all items in the folder (the latter part most likely being a "for each item in folder" type of thing).

Any thoughts, insights?

Best regards,
Ole Andreas Ringdal

W7Ux64, VS 2008 Pro, ADX.NET 2009.5.2.2024
Visual Basic
Posted 21 Oct, 2009 05:51:45 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Hello Ole,

Note that adxOutlookEvents_FolderContextMenuDisplay will be called in Outlook 2007 only. You need to cast the target parameter to Outlook.MAPIFolder and then get the Items collection via MAPIFolder.Items. It isn't recommended to use foreach when looping through Outlook items, use a FOR loop instead. Please see Why to release COM objects in Office add-ins?.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Oct, 2009 06:26:10 Top
Ole Andreas Ringdal


Rather green-ish


Posts: 46
Joined: 2006-05-30
Hello Andrei, thanks for the prompt reply, as always. Yes, I'm developing for Outlook 2007, to clarify that.

Cast to Outlook.MAPIFolder? I tested "If TypeOf target Is Outlook.Folder Then [...]" and that seemed to work. In adxOutlookEvents_FolderContextMenuDisplay, that is.

But using the "do stuff" code should be in the handler for the context menu item, not in the display event handler, and how do I do that? Could you please provide a quick example?

And I'll try to "marshal" myself to sift through that document you linked.

Thanks again. Best regards,
Ole Andreas Ringdal

W7Ux64, VS 2008 Pro, ADX.NET 2009.5.2.2024
Visual Basic, Office 2007
Posted 21 Oct, 2009 08:01:45 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Hello Ole,

In adxOutlookEvents_FolderContextMenuDisplay you can get the folder being right-clicked and save it to a variable which you access in the Click event of your context menu item, then do your stuff, and release the variable.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Oct, 2009 08:23:38 Top
Ole Andreas Ringdal


Rather green-ish


Posts: 46
Joined: 2006-05-30
Andrei, thanks again.

Sorry for the newbie question, but what kind of variable to I save it to? An object? I thought of saving the folder name to a string, and then perhaps search for a folder named that, but I realized that was just me veering off in an totally idiotic direction...

Thanks again. Best regards,
Ole Andreas Ringdal

W7Ux64, VS 2008 Pro, ADX.NET 2009.5.2.2024
Visual Basic, Office 2007
Posted 21 Oct, 2009 08:29:01 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Ole,

Outlook.MAPIFolder, or Outlook.Folder in Outlook 2007.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Oct, 2009 11:00:17 Top
Ole Andreas Ringdal


Rather green-ish


Posts: 46
Joined: 2006-05-30
Hello Andrei,

I can't get this to work, and it seems that the folder I store in a variable doesn't get stored, or is released when adxOutlookEvents_FolderContextMenuDisplay exits. Could you please have a quick look and see where I fail here? Thanks in advance.

Public Class AddinModule

[...]

Dim objRightClickFolder As Outlook.Folder

[...]

Private Sub adxOutlookEvents_FolderContextMenuDisplay(ByVal sender As Object, ByVal commandBar As Object, ByVal target As Object) Handles adxOutlookEvents.FolderContextMenuDisplay
Try
objRightClickFolder = CType(target, Outlook.Folder) 'Is this the right type casting?
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub btnShowFolderName_Click(ByVal sender As Object) Handles btnShowFolderName.Click
Try
MsgBox(objRightClickFolder.Name) 'Gives the "COM object that has been separated from its underlying RCW cannot be used" error
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Variable objRightClickFolder declared in class, but I'm not sure if that's the right place.

Thanks again. Best regards,
Ole Andreas Ringdal

W7Ux64, VS 2008 Pro, ADX.NET 2009.5.2.2024
Visual Basic, Office 2007
Posted 22 Oct, 2009 06:21:07 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Hello Ole,

You are right, it is released as soon as the event handler finishes. Try another way:
- in FolderContextMenuDisplay, get the EntryID of that folder,
- get the StoreID
- store them in two variables
- in the Click event, use Namespace.GetFolderFromId to get the Folder object:

dim ns as Outlook.Namespace = OutlookApp.Session
dim folder as Outlook.Folder = ns.GetFolderFromID(folderEntryID, storeEntryID)
marshal.ReleaseComObject(ns)
'your stuff here
Marshal.ReleaseComObject(folder)


Does this work?

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 22 Oct, 2009 07:07:15 Top
Ole Andreas Ringdal


Rather green-ish


Posts: 46
Joined: 2006-05-30
Hello Andrei,

Yes, excellent, this worked perfectly, and I can now get to the folder being right-clicked on. Thanks a lot for your help, I really appreciate it.

By the way, why is the folder object released when FolderContextMenuDisplay exits? I did set it to a global variable, or did I do that wrong?

Best regards,
Ole Andreas Ringdal

W7Ux64, VS 2008 Pro, ADX.NET 2009.5.2.2024
Visual Basic, Office 2007
Posted 22 Oct, 2009 15:05:29 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Hello Ole,

Add-in Express releases that parameter because otherwise developers will be obliged to release that parameter themselves. In that case, the developers creating, say, an empty event handler for FolderContextMenuDisplay, might have problems for a hardly understandable reason.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 23 Oct, 2009 08:56:43 Top