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
Related
I'm trying to do something that I'm not sure is possible.
I want to use a .net 3rd party library that gives nice winform chart controls, to replace some (very complex and ugly) VBA generated charts in an Excel worksheet.
I'm using Excel-DNA (which is great BTW), and I managed to do all sorts of things with it, like displaying Winform forms in both sync and async way (thanks to https://github.com/Ron-Ldn/DotNetRefEdit)
So far, I managed to make my non modal form with the charts appear when I activate a particular worksheet, and by setting it's parent and topMost properties, it kinda have the expected behavior. But placing it is a real pain (different screen sizes) and I was wondering if there is a way to properly do it
So here's the question : Can I attach in some way a .Net form or a custom control to a worksheet, maybe even have it positionned relatively to a particular cell ? It must be non modal and it will display only if we are on a particular sheet.
I know with VSTO you could add a control to a worksheet, but I don't want to use VSTO. I also know that I could probably create my own ActiveX control and have it registered, and then place it in Excel like I would with another button control for example, but that seems hazardous according to what I read about it
Thank you for all ideas or suggestions
Consider creating a custom task pane (CTP) pinned to the right hand side of the Excel workbook (worksheets).
For more information on how to do this in ExcelDNA, see ExcelDNA GitHub Sample on CTP.
I am updating a Windows Forms application that was written for Office 2003. It would open Word, populate it with text from the database, create buttons appropriate for the user's security roles, and handle button clicks. The old way was with CommandBars. The Word documents created this way were stored as DB Blobs, so they were only ever available via the Forms application. That is the functionality I need, but without CommandBars.
Now, we have the Ribbon, Action Panes, VSTO, and OpenXML. I know my way around C#, but I'm new to all of these technologies.
I imagine a solution where I set up a template with the proper buttons on a nice ribbon, and the buttons raise events. If someone just tried to use the template by itself, there would be no data and the events would do nothing. But my Windows Form could create a new document based on the template, populate it with text from the database, hide inappropriate buttons, and handle button click events. Easy peasy.
I began by adding a Word 2010 Template project and making a ribbon for it. It looks nice in the debugger. But, I can't create a new document with it from a Console app. Word launches, but the ribbon isn't there. There's a note in the documentation that "If a Microsoft Office Word template has managed code extensions, the project assembly is not called if the template is attached as a global template or loaded from the startup directory of Word. In addition, the document does not recognize the format of a template that is part of an Office solution." I believe this means my template is useless. I can't open Word and make a new document with my template or call the template from another project, so, pointless.
All the MS help has failed me, and mostly leads me in circles. I need some direction. For each piece of the scenario I described, what is the most appropriate modern technology? For each technology I need to use, what is the best resource for my situation?
We use a 3rd party Excel Addin to populate data in an Excel worksheet. I've been given the task of writing a C# app (WinForms or VSTO) that will automatically click the Ribbon button and process the data.
The problem is I can't find any way to automate clicking the Ribbon button (on the Add-ins tab) using C# code.
I've also tried using Win32 API's to send key presses to the parent Excel window (using SendMessage), but the Key presses weren't received. (I checked using Spy++).
And looked at the public methods exposed in the Add-in assembly, but it didn't contain any public methods to do the same as clicking the ribbon button.
Does anyone know how I can click a ribbon button in the Excel (add-ins) ribbon using C# code?
Look at the SendKeys method of the Excel Application class. It allows you to send the keys directly to Excel. You can press the Alt key to see which shortcuts are assigned to which buttons / ribbon tabs. I automated some tests using this and it worked fine.
If you needed even more complex things you could also look into Sikuli. It's based on image recognition and is using Java.Robots class for sending keypresses and clicks. Automating UI tasks is quite easy with it.
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.
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.