Opening c# word 2013 project from inside Windows Form - c#

I have successfully built a C# Word 2013 project (ReportGenerator) that opens an MS ACCESS database and generates a MS WORD 2013 report. The results are very good. The issue I have is at the moment it can only be run from inside Visual Studio. My boss wants it to run via a windows form.
I have the competence to build a new project (ReportRunner) that contains a windows form with a datagrid, populate it and put a button on it. What I lack is the competence to know how to:
Open the report generation code from ReportGenerator in the
onclick event of ReportRunner
Pass a variable from ReportRunner to ReportGenerator so to avoid
hard coding.
I was expecting to be able to write a line like “ReportGenerator.ThisDocument.ThisDocument_Startup” in the click event of the button. This isn't happening.
The significant bits of code in my projects are:
ReportGenerator
namespace ReportGenerator
{
public partial class ThisDocument
{
ReportData reportData = new ReportData();
public void ThisDocument_Startup(object sender, System.EventArgs e)
{
int idToLookFor = 2;
reportData = MyFunctionToReadAccessData(idToLookFor);
MyFunctionToPutDataIntoReport();
}
}
}
ReportRunner
using ReportGenerator;
namespace ReportRunner
{
public partial class Form1 : Form
private void button1_Click(object sender, EventArgs e)
{
int idToLookFor = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
//HOW DO I MAKE IT OPEN REPORT GENERATOR ThisDocument_Startup
// AND PASS IT THE idToLookFor
}
}

Update:
I'm having trouble understanding your comment so here's a few updates:
You can call method from a Document-level Addin from a seperate C# WinForm using the link I provided. It doesn't matter if it's an Application-level addin or a Document-level addin - the approach is the same. See this link.
Why did you build a ReportRunner Form project that is separate from your ReportGenerator Add-in project? As I said below, you can create a single VS solution with 2 projects - one is a Document-level addin, the other is a WinForm and you can simply call the WinForm from the Ribbon associated with the addin.
I assume that you're asking how to call a function from a Word Addin from a Winform? I recently explained how to do this here: How to call a VSTO AddIn method from a separate C# project?
That being said, I don't recommend doing this becaues you can simply package your WinForm together with your Addin and then open it like this using a Ribbon:
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Form1 aForm = new Form1();
aForm.Show();

Related

Switch between multiple windows

We have a software system that should support multiple variants. Each variant should contain a customized version of one or more UI components (Windows Forms in this case).
A prototype has been created in VS2017 and has the following solution;
ProductFoo (Solution)
MainApplication (Windows Application)
MainApplicationForm1.cs (Windows Forms)
XY1 (Class library)
Form1.cs ((Windows Forms)
XY2 (Class library)
Form1.cs ((Windows Forms)
In this simple prototype, the MainApplicationForm1 forms contain one button that when clicked should either show Form1.cs in XY1 og XY2 library depending on which variant is selected.
To solve this we have updated Solution Manager with following solution configurations;
XY1_Debug
XY1_Release
XY2_Debug
XY2_Release
Then we added conditional compilation symbols for MainApplication.
The solution configurations XY1_Debug and XY1_Release use the conditional symbol XY1
The solution configurations XY2_Debug and XY2_Release use the conditional symbol XY2
Then we added reference from MainApplication to both XY1 and XY2 projects.
Lastly, we added the following code in MainApplicationForm1.cs
public partial class MainAppForm1 : Form
{
public MainAppForm1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
#if XY1
XY1.Form1 form = new XY1.Form1();
form.ShowDialog();
#elif XY2
XY2.Form1 f1 = new XY2.Form1();
f1.ShowDialog();
#else
#error The MainApplication is missing Form1
#endif
}
}
This solution works but I have reservations with using preprocessor directives. The code looks messy and can quickly become difficult to maintain. What are the best practices for this kind of scenario?
Appreciate any input.
Your question is quite broad and referes to the base structure of the project you want to have.
The way you choose is close to the Feature toggling, just done based on the build configuration. Ususaly it sould be something like:
if(features.IsOn("XY1-feature")){
XY1.Form1 form = new XY1.Form1();
form.ShowDialog();
}
Classical way can give you more flexibility. E.g. moving feature toggless to the config would give you a possibility to dynamically toggle different features for specific deployment, but, as impact, it would encrease the complexity and would require more testing
I would suggest you to take a deeper look into Dependency injection and Strategy pattern
As an alternative to the Feature toggling you can use branching. Create a specific branch for the specific project/client. That could bring you problems with merging, but would keep your cleaner for a specific implementation. It would fit best to the project with lots of minor differences from project to project
I suggest using two radio buttons to solve this problem. This is a very easy way.
Select radioButton1, pop up XY1.Form
Select radioButton2, pop up XY2.Form
MainApplicationForm1.cs:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
XY1.Form1 form = new XY1.Form1();
form.Show();
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
XY2.Form1 f1 = new XY2.Form1();
f1.Show();
}
}

What is the simplest way to run nunit 3 tests from a button in a Windows Form?

I currently have an nunit project outputting a class library 'RegressionTests.dll' that opens the Selenium WebDriver and runs a few dozen UI tests. I have created a WinForm app with a button 'Run Tests'. When clicking this button, I want to execute a series of n-unit tests from RegressionTests.dll.
I had gotten this to work on my local machine using Process.Start("nunit3-console.exe, nunit-console RegressionTests.dll"), but realized that it would only work on my local if I had installed nunit3-console as a standalone app. After realizing this, I dug more into the n-unit documentation and discovered the n-unit engine. I have tried leveraging the n-unit3 Engine in order to run it internally but have faced issues with implementation of the ITestEventListener in the WinForm project. I've attached the code to my button here:
Form1.cs
private void btnRun_Click(object sender, EventArgs e)
{
TestRunner.Run();
}
Inside TestRunner.cs, we have this code:
[Extension(Description = "Test Reporter Extension", EngineVersion = "3.11")]
public class TestRunner : ITestEventListener
{
public static void Run()
{
ITestEngine engine = TestEngineActivator.CreateInstance();
TestPackage package = new TestPackage("RegressionTests.dll");
ITestEventListener testListener = new TestRunner();
using (ITestRunner runner = engine.GetRunner(package))
{
XmlNode result = runner.Run(testListener, TestFilter.Empty);
}
}
public void OnTestEvent(string report)
{
throw new NotImplementedException();
}
}
Currently, the solution layout is as follows.
Solution
Regression (project)
RegressionTests.dll
TestRunner.cs (file that contains my code linked above)
SeleniumFormApp
Form1.cs (contains button that, upon click, should run Selenium test cases)
How can I leverage n-units Nuget packages to accomplish what I want to here? Is n-unit engine the proper one? If so, how should the ITestEventListener be implemented to accomplish this?
Thank you - please let me know if this is unclear.

Visual Studio 2013 not finding code after I moved it to another file

All my code was in one file, say, Form1.cs. The file was getting too big so I decided to break up my code into several new files. I used the solution explorer to add the new files. (right-click > Add > New Item... > Code File)
So for example, I created SomeFolder/ClickHandlers.cs, and cut my someButton_Click() function from Form1.cs and pasted it into said new file. So the new file looks like this:
using blah;
using blah...;
namespace FooApp
{
public partial class Form1 : Form
{
void someButton_Click(object sender, EventArgs e)
{
// Do things..
}
}
}
And Form1.cs no longer has this function in it. My program runs perfectly fine. But VS didn't seem to get the memo. When I go into the designer, select "someButton" and then go into the events list in the Properties Pane, I see my function "someButton_Click" in the Click event. When I double-click the function, it takes me to the beginning(ish) of Form1.cs, not into SomeFolder/ClickHandlers.cs where the function got moved to.
How can I make VS learn the new locations of my code so I can be taken to it from the designer? I've tried deleting the .suo file and the obj folder. No dice.

Microsoft Ribbon button to execute function from add-in

OK, so I've done a lot of googling trying to find information on this topic and I've come up pretty much empty handed. Maybe I'm not searching for the correct terminology for what I'm trying to accomplish.
My issue is that I've written a function in a MS Excel add-in, I followed the instructions from Microsoft as a starting point, but their tutorial has the code execute every time the user saves the document. My goal is to have a button on the ribbon I designed execute this function rather than the save button.
This is the Microsoft article that I followed to get myself started: https://msdn.microsoft.com/en-us/library/cc668205.aspx
I also found this question on here, but it didn't have enough detail for me to figure out how to implement the solution for myself: How to connect a ribbon button to a function defined in an Excel add-in?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace ExcelAddIn1
{
public partial class ThisAddIn
{
void FormatTime(Microsoft.Office.Interop.Excel.Workbook WB, bool SaveAsUi, ref bool Cancel)
{
/////MY FUNCTION BODY HERE//////
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Thanks in advance for your assistance.
VSTO provides two ways for creating a custom UI:
The Ribbon designer - see Walkthrough: Creating a Custom Tab by Using the Ribbon Designer.
A raw XML markup - Walkthrough: Creating a Custom Tab by Using Ribbon XML.
In both cases you may access the add-in properties and methods using the Globals.ThisAddin property which returns an instance of the add-in class (shown in your code listed above).
Usually you can use Globals.ThisAddIn.Application to access application level and document level UI.
I hope this link can help. Here is a sample of adding a button to a worksheet like this:
Globals.Factory.GetVstoObject(
Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1])
.Controls.AddControl(button, selection, buttonName);
Looks like

c# simple skype app throws COM exception

I am making my first Skype app that can simply message a user but when I debug I get a exception that crashes my app.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using skype_app;
using SKYPE4COMLib;
namespace skype_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
var oskype = new SKYPE4COMLib.Skype();
oskype.PlaceCall(textBox1.Text);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var oskype = new SKYPE4COMLib.Skype();
oskype.SendMessage(textBox1.Text, textBox2.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
i have use some extra references
references list:
Microsoft.Csharp
SKYPE4COMlib
SkypeDialoglib
system
system.core
system.data
system.data.DataSetEXTensions
system.deployment
system drawing
System.Windows.forms
System.xml.linq
Here is the exception i get:
System.RUntime.InteropServices.ComException : {"connection refused"}
So I guess my main question is why does my connection get refused when Skype dose not even open the dialogue asking if I want to allow the connection ?
The issue is that you're trying to debug in Visual Studio. Unfortunately, according to Skype themselves, they do not support using this API & debugging in VS:
Per the link:
The most comment cause for this is you are trying to debug the program
in Visual Studio. Going forward we will not be able to support using
the visual studio hosting process for debugging. You can turn it off
by:
Open your project in VS
Open your projects properies
click the debug tab
untick "use visual studio hosting process"
rebuild your application and begin debugging and it should work ok.
i face the same issue. this way i solved it.
here is my code
Skype skype;
skype = new SKYPE4COMLib.Skype();
Call call = skype.PlaceCall(txtPhonenNo.Text);
first thing login to skype and go to Tools > option > advanced settings
your screen would look like
click on manage other program's access to skype
then another window will come which will show all program name which try to access skype. if any exist just select all and remove it.
then run your program again and go to that screen where this option was available called click on manage other program's access to skype
click there and a windows will come which will display the name of your apps just select that name and click on change button then another window will come which looks like
in that window just select the option called allow this program to access skype then a dialog come on the skype window which looks like
where you need to click on allow access button and then your job will be done. hope this will help.

Categories

Resources