Minimise winform without blocking Outlook functionality - c#

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

Related

When form’s owner is busy, form is busy too. But I don’t want that behavior

I am writing a macro for an application. I can get the window handle of the application so my macro form has an owner. So the windows of application and my form are paired.
My macro creates a list of tasks and one by one sends that to the application. So my form is user responsive when working. The application does long computing (It’s CAM software) and I added a button that can stops sending tasks to the application.
It’s work good, when my form has no owner, but then windows aren’t paired.
When windows are paired, the busy status is copying to my form. So clicking to my stop button is clumsy.
Has anybody some suggestion, how a form can have an owner, but only for pairing?
(By pairing I mean: When I click on the window of application, my form is binged front too and vice versa.

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.

C# Windows not opening on top

I've written a relatively large application with lots of dialog boxes and forms, etc.
I'm opening them with Form.ShowDialog().
A lot of the time, the forms open behind existing windows, e.g. yesterday I was testing it on a machine with several other programs open: many Windows Explorer windows, a few Excel windows, etc. A lot of my forms, open/save file dialogs, etc were supposed to open but didn't. I was twiddling my thumbs until I pressed alt+tab and realised that they were, in fact, behind another window.
Why is this happening, and how can I stop it in future? Thanks.
Use the ShowDialog override which takes an owner window as a parameter.
By passing in your main window as the dialog's owner, you guarantee that the dialog always pops in front of it, and stays in front of it.
And it won't annoy the user if they were using some other application.

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.

How do i close all open windows from my C# program?

Hi i have a windows application where i show a webbrowser, i have a button that removes the browser (its a preview) and goes to another "view" in my application (another tab). My problem is that my users are getting advanced, they build HTML with links (and its ok) but the links may spawn new browser windows (IExplorer), and my button needs to close these windows, but how?
I have made some code to traverse all eht windows that ends with "Windows Internet Explorer", ahd it all seems to work - but how do i close them? I am trying to do it like this:
SendMessage((int)hWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
It seems to work, but the browser pops up a dialog asking me if i want to close the tab of all the tabs...how to work around/solve this?
Cheers,
walking over all top level iE windows and closing them is a bad idea, unless you are guaranteed users can't launch ie and browse the Internet on their own. Otherwise, you might actually lose user data (say an email or a blog post the user has been working on in the last half an hour)
You can't easily work around that dialog without modifying the per-user IE settings. Your other option is to look for that dialog and click the yes button, but that would be fragile and is not guaranteed to continue working if the user upgrades to IE9.
You could potentially prevent opening links in new window by listening to BeforeNavigate event and allowing only navigations that are guaranteed to happen in your control. However, there are scenarios where IE might still decide to open new window.

Categories