C# VSTO Outlook 2007 / Sharepoint: AddIn wont save contact item - c#

can somebody help: i have an AddIn which reads contacts from a Sharepoint Server.
The user can load a contact and make some changes or create a new one. At his point, saving the item works well. The Form closes and the item is beeing transfered to Sharepoint.
But if the user reopens the same contact again and trys to make a simple change, saving is no more possible: The element could not be saved because it was changed by another user or window. Would you like to place a copy in your standard folder" (this a my translation from german to english...) The user must restart Outlook to make his change! I dont know, why the "fxxx" this happens.
I hook into the "Write" event to check some rights and if all is ok i do a
Marshal.ReleaseComObject(item);
this.Dispose();
and call the Garbage Collector:
GC.WaitForPendingFinalizers();
GC.Collect();
Even i write these lines into the FormRegionClosed-Method the problem stays the same.

Yeehaaaa: found the problem!
in AddIn-Startup a have an event handler for setting the message class on the current item.
This item has to be Marshall'd too

Related

Minimise winform without blocking Outlook functionality

I am working on a vsto plugin in which I've created a Form(WinForm) that has fields for long text that user has to copy from previous mails. Now, the issue i am facing is, whenever I minimise the winform, the whole Outlook gets minimised. And when I try to open the outlook application from taskbar, it goes to form again.
Is there a way where user can minimise the Form and work on outlook application independently?
For example, he/she might need to scroll through few mails to get some values of field in the winform more than one time, so they minimise the form, fetch the long text, then open the form again and paste it.
Create a task pane instead of a floating window - see https://learn.microsoft.com/en-us/visualstudio/vsto/custom-task-panes?view=vs-2022&tabs=csharp

How to get save confirmation dialog result of outlook

I don't know how to get result of save confirmation dialog of outlook (in C#).
This confirmation box is opened up by following:
Open mail in outlook
Made some changes in content
Close the mail
Please refer this image
I want to get this opened save confirm box and its result.
Thanks in advance.
Track the MailItem.Close / Write / AfterWrite events - Close will fire immediately before the prompt is shown, Write fires before the message is saved, AfterWrite immediately afterward.
There is no trivial way of getting such information. But you may track the sequence of events fired for the item and realize the answer following that way.
First, to handle item-level events you may develop a wrapper for an Outlook item like described in the Implement a wrapper for inspectors and track item-level events in each inspector article.
The MailItem.Close event is fired when the inspector associated with an item (which is an instance of the parent object) is being closed.
The MailItem.Write event is fired when an instance of the parent object is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector).
So, if you have got Close fired following by the Write event - that is your case!

C# VSTO Outlook 2013 - Explorer Close handle Save Event

I have a RibbonBar with some Buttons that appears in the Outlook Explorer (Double Click on a mail).
Some progresses in the background edit the attachments of the mail. If the user close the Explorer, outlook will ask to save the edited mail (save the changes).
is there a Event to handle this? i don't want that the message appear if my programm has changed the mail.
I wasn't succesfull on the search for an Event. But maybe i just missed something.
There is no need to handle any event for this.
If the user close the Explorer, outlook will ask to save the edited mail (save the changes).
This is a good indicator that changes were not saved or Outlook objects (actually underlying COM objects) were not released correctly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about that in the Systematically Releasing Objects article in MSDN.
Some progresses in the background edit the attachments of the mail.
Don't use the Outlook object model from secondary threads. Office applications (Outlook in your case) use the single threaded apartment model. You can gather the required info on the main thread (keep it in a string for example) and process it on another thread. Or you can use a low-level API (Extended MAPI) which allows to run multiple threads. Also you may consider using any wrapper around that API. Frankly speaking, Outlook is a big wrapper around that API.

Shared addin (Outlook currently) - toolbar button works a bit, then stops, no errors given

Bit of a weird one this: I've got a shared Office addin that currently targets Outlook 2007 on XP and Win7 (Excel/Word and other versions later). It adds a toolbar button, and this fires off my code. My code just reads data from Outlook. First time round, it works a treat. Second time, the button click does nothing. No errors, nothing. The rest of Outlook responds as normal.
The entire event code is in a try/catch that writes any exceptions out to a temporary file (this mechanism works if a throw an exception in code), but nothing is picked up.
According to the "Trust Center", my addin is active (not inactive or disabled). If I restart Outlook, or stop and start the addin via the Trust Center, it again works once and then stops.
If I reduce the amount of work my code does, the button will sometimes respond for about ten clicks or so, then gives up. Whichever version of my code I try, resources don't seem to change much (memory moves up a mite, the thread count stays the same).
I've read of a similar thing with Word, but this was explained by Word recreating the toolbars or menus every time it loads a document (not sure how correct this is). For me, I select an email, click once, it works, click again (no changing views in Outlook, selecting different emails, etc), and it doesn't.
Has anybody got any clues here?
You should place the variable that contains your toolbar buttons to a global scope (You can keep then as fields of the addin object). If you don't keep a valid path to them at all times, .NET garbage collector will eventually clean up these objects and the event handlers with it. This will however not remove the visible button so it will still be visible.

Can not create CommandBar when Outlook is run minimized (Outlook 2007 Add-In)

Original post:
When Outlook is launched, the Add-In is loaded and adds a toolbar with some buttons.
toolBar = OutlookApp.ActiveExplorer().CommandBars.Add(MENU_TAG, MsoBarPosition.msoBarTop, false, true);
Everything was working fine, but now one user has his Outlook shortcut set to launch Outlook minimized.
And then OutlookApp.ActiveExplorer() return null.
Is there some event I can use to catch when there is an ActiveExplorer and then add the commandbar?
OutlookApp.Explorers.NewExplorer doesn't work.
Also, when I show a messagebox before I add the CommandBar: everything works fine, even with Outlook minimized... Why?
edit:
Accessing the Explorers proprerty directly did work, as someone said in the answers. So this solves the problem for a minimized outlook... But...
One of the users does not have Outlook run minimized, and still the plugin loads before any gui is available. There are not even 1 explorer in the Explorers collection :( How is this possible?
edit 2:
I tried using a timer as suggested by 76mel, checking ActiveExplorer for null every 100ms. This adds the buttons as expected, but I can set the Picture property of the button.
I get this exception:
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
If there is no UI = no explorer :(
Try waiting until the Explorers.NewExplorer event fires to be able to get a CommandBars object.
Update:
Yes it looks like a timer will do the trick ok a bit hacky.
So wire up a timer when you have a null ActiveExplorer and check for the ActiveExplorer onTick. Once the user pops outlook you get you active explorer and you can then add you tool bars.
I'm not familiar with managed addins, but I found this answer.
If there is no ActiveExplorer, try to access the Explorers collections directly, like in Explorers[1].
I had the same problem in my ECE and solved it by waiting for OnObjectChange callback that would be called when the user changes a folder in Outlook, and then I try to recreate the toolbar. This might roughly correspond to the FolderSwitch event in the Outlook object model.
Just my 2c.

Categories