简体   繁体   中英

Opening c# word 2013 project from inside Windows Form

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:

  1. Open the report generation code from ReportGenerator in the onclick event of ReportRunner
  2. 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:

  1. 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 .

  2. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM