c# : VSTO -> MS Office Apps loosing focus? - c#

So, what is happening, that Im loosing the focus from the Excel App to the PowerPoint App ?
UPDATE:
Your answers are correct (+1 Point), however its my fault, for not explaining the complete problem (I thought it is much simple).
So, the problem statement expands to : From a MS PowerPoint, I open MS Excel, from where I open the ColorDialog. So, the problem is to gain the IWin32Window owner (or its handle) of the calling Excel App.
I used a modified solution from How do you pass the owner window to Show() method overload? to find the IWin32Window owner, and it works.
However, even doing so, the focus goes back to the parent PowerPoint App.
So, what is happening, that Im loosing the focus from the Excel App to the PowerPoint App ?

If you are launching the form from a different form, you should set that as the Owner of the ColorDialog:
if (dlg.ShowDialog(this) == DialogResult.OK) //"this" being the owner form

Related

c# VSTO Host WPF form inside Excel sheet

I would like to embed a standalone WPF form inside an Excel sheet.
Microsoft provide information on how to host a WPF form inside an Excel Action Pane or Task Pane here: https://msdn.microsoft.com/en-us/library/bb772076.aspx
However, I'm unable to find information on how I can do this within the Excel sheet itself. Is this possible? Should I just load the form when the user clicks on the tab? Or is there a better way to do this?
I've never seen that anywhere so I guess it's not possible.
What I have done a few times (but never in production) is your second suggestion
Should I just load the form when the user clicks on the tab? Or is there a better way to do this?
Yes, this should work fine, there is something like WPF host you can use on standard form or create another project in your solution (WPF project) and call it from VSTO

How to not allow to switch between word instances if any modal window is opened using vsto?

I have created a word addin using VSTO in vs2010 and it is working fine. But i am faceing a handicap issue, If I open my Windows modal dialog in word then it should not allow to switch between multiple instances of word.
I want same behavior of my modal dialog as showdialog/openfiledialog in MS Word.
I already have modal dialog by setting owner property with win handle in wpf or via showdialog(hwnd).
but still i am able to switch between word documents.
thanks in advance.
sumit sharma
You need to use the ShowDialog method of the Form class and specify the parent window handle using the IWin32Window interface. The Window class from the Word object model provides the Hwnd property which returns an integer that indicates the window handle of the specified window. You can use it to create an instance of the IWin32Window interface and pass it to the ShowDialog method.

Disabling applications for form tasks (C#)

I'm creating an add-in for Microsoft Excel that helps to automate some of my accounting processes. One of the pieces of the add-in is a ribbon tab with a button. Clicking this button opens a form with a list box that displays each account (worksheet) name and their respective balances.
The problem is that the rest of the controls in Excel are still tangible while the form is open. If, for example, a person were to change the name of the worksheet while the form was open, it may cause an error.
I noticed that message boxes render an application unclickable until they exit the screen. It isn't limited to just message boxes, either- I've seen the Nexus Mod Manager do the same thing when installing mods for games. I've seen it in many applications, but I haven't figured out how to do it myself.
My question is simple: how do I change the properties of a form such that the rest of the application is disabled until the form is closed?
What you need is a modal form. Opening a modal form will "block" the entire UI until it is closed, just like message boxes do.
Have a look at http://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx

Windows Forms above Excel

I have a form which spawns from an excel add-in written with VSTO.
I would like the form to remain always above the excel spreadsheet, but still allow the user to interact with the sheet below it.
The forms constructor includes this.TopLevel = true;, but the form still disappears behind the spreadsheet when I select a cell.
Has anyone else come across this problem and found a workaround?
A form already has TopLevel set to true. You want TopMost.
P/Invoking SetParent() would be best but getting the window handle you need might not be that easy. Maybe Process.GetCurrentProcess().MainWindowHandle
if you want to have an interactive form in Excel you'll need to do some pinvoking as Hans said in the original answer.
see this link - on MSDN
This example is for the refedit control (which is normally only available from VBA).
The example should give you the necessary information on IWin32Window and pinvoking to call the Show method of the form object with this parameter value.

Can you open a form or window in an Outlook Addin (VSTO)

I am new to VSTO programming. I have created a basic addin for Outlook 2007 that monitors a folder containing XML text files which it opens and then sends them as an email, then deletes them. this all works fine.
I want the user to be able to configure certain settings for the way the addin/program will operate, such as the folder that it will monitor, and other things. The logical way to do this is to create a menu item in the addin (which I have also done) that opens a windows form (or XAML window) that allows them to enter the parameters.
In my addin I added a new item Windows Form, which worked, and the designer opened. However, in my addin code I cannot open the form. The Show() method normally associated with form objects is not available.
Is this simply something you cannot do, or am I just doing it the wrong way?
I have read about Outlook form regions, but these seemed to be attached to outlook items such as a new email, task, appointment etc... there doesnt seem to be a way to create a form region that can be opened in the main window of Outlook.
Ideally, I would like to go with my original method of opening a new window from a menu item, but if this isnt possible I would like to hear other solutions.
Thanks,
Will.
For a normal form, it sounds like you didn't just add System.Windows.Forms as a reference,
create the object then show it eg.
Form myFrm = new frmFlightList();
myFrm.Show();
This should work in a VSTO addin, as it does in any other form. The CMSConnectorControl object you refer to is a distraction to others for the general case of just wanting to display a form.
figured this out, After I built my form I just had to add these lines
CMSConnectorControl formMain = new CMSConnectorControl();
formMain.ShowDialog();
to the ThisAddin_Startup() function.

Categories

Resources