none
Opening C# Word 2013 project from Windows Forms RRS feed

  • Question

  • 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 going to happen.

    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

    }

    }

    Thursday, July 9, 2015 8:56 AM

Answers

  • Hi opc

    Typically, you'd need to store the idToLookFor somewhere that button1_Click can pick it up. For example, you could create a Web Service, have ReportRunner provide the value to the WebService and have Report Generator call the value from the web service.

    But it could also be written to and read from a text or XML file.

    I suppose it should also be possible to expose part of the Windows Form application so that the Report Generator could call it directly, but I'm not sure about that - you'd need to ask in a Windows Form forum about whether and how it could be done.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 9, 2015 1:44 PM
    Moderator

All replies

  • Hi opc

    Typically, you'd need to store the idToLookFor somewhere that button1_Click can pick it up. For example, you could create a Web Service, have ReportRunner provide the value to the WebService and have Report Generator call the value from the web service.

    But it could also be written to and read from a text or XML file.

    I suppose it should also be possible to expose part of the Windows Form application so that the Report Generator could call it directly, but I'm not sure about that - you'd need to ask in a Windows Form forum about whether and how it could be done.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 9, 2015 1:44 PM
    Moderator
  • Thanks,

    Your right , It would be sensible to store the ID somewhere. It's now being stored in the access database under a different table!

    I will keep trying to find out how to access the "main" method of my word document!

    Friday, July 10, 2015 3:47 PM
  • Hi opc

    This is getting a bit off-topic for the VSTO forum, but...

    You can reference the DAO or OLEDB DLL that Access provides from your VBA project and retrieve information directly from the Access database.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 10, 2015 7:54 PM
    Moderator