1

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
      }
}
opc51
  • 13
  • 1
  • 6

1 Answers1

0

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();
Community
  • 1
  • 1
Charlie
  • 2,004
  • 6
  • 20
  • 40
  • An Addin is an application level object that has functionality that is applicable to multiple Document level objects. This isn't what I have done. I have written a "Document" level object that contains pre-written text and tables. ReportGenerator namespace reads data from Access and places it in that document. My issue is that I need to open this document from a ReportRunner namespace that contains a form that has a list of all the possible datasets that could go into this specific document. This would mean that someone can generate the report without having to have visual studio installed. – opc51 Jul 09 '15 at 15:13
  • i'm not building an Add In (as I understand it) , I have an existing report with text and blank tables. I created a c# program that reads in data from Access and puts it into the report and renames. (This works great). - this is called ReportGenertor What I need is to open this report from a Winows Form. I build a form put in grid view, populated it with data I need. This form has a button on it. – opc51 Jul 10 '15 at 14:54
  • when I press the button I want it to call upon ReportGenerator which contains all the code for putting the data into the Word document. I cannot access the ReportGenerator method thisDocument_startup from ReportRunner. This method appear to be the "main" method of the word document. If I am unable to call this method then I don't know how to tell ReportRunner to call upon ReportGenerator and start putting data in. – opc51 Jul 10 '15 at 15:03
  • I currently run ReportGenerator from Visual Studio. I want it to be fired off by the windows form ReportRunner so that non coders people canfill the reports with access data. – opc51 Jul 10 '15 at 15:09
  • You shouldn't put anything in thisDocument_startup. Your code should be in a separate method (e.g. CreateVstoNamedRange) as explained in the link above and also this link. Please walk through this link and clarify what questions you have. https://msdn.microsoft.com/en-us/library/vstudio/bb608613%28v=vs.100%29.aspx – Charlie Jul 10 '15 at 18:30
  • If you haven't created a Document level Word addin that what type of Visual Studio project is your ReportGenerator? The fact that it has a thisDocument_startup suggests that it is an addin. – Charlie Jul 10 '15 at 18:33