简体   繁体   English

从Windows Form内打开C#Word 2013项目

[英]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. 我已经成功构建了一个C#Word 2013项目(ReportGenerator),该项目可打开MS ACCESS数据库并生成MS WORD 2013报告。 The results are very good. 结果非常好。 The issue I have is at the moment it can only be run from inside Visual Studio. 我目前遇到的问题是只能在Visual Studio内部运行。 My boss wants it to run via a windows form. 我的老板希望它通过Windows窗体运行。

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. 我有能力构建一个新项目(ReportRunner),该项目包含带有数据网格的Windows窗体,将其填充并在其上放置一个按钮。 What I lack is the competence to know how to: 我所缺乏的是知道如何做到的能力:

  1. Open the report generation code from ReportGenerator in the onclick event of ReportRunner 在ReportRunner的onclick事件中从ReportGenerator打开报告生成代码
  2. Pass a variable from ReportRunner to ReportGenerator so to avoid hard coding. 将变量从ReportRunner传递给ReportGenerator,以避免硬编码。

I was expecting to be able to write a line like “ReportGenerator.ThisDocument.ThisDocument_Startup” in the click event of the button. 我希望能够在按钮的click事件中写一行“ ReportGenerator.ThisDocument.ThisDocument_Startup”。 This isn't happening. 这没有发生。

The significant bits of code in my projects are: 我的项目中的重要代码如下:

ReportGenerator 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 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. 您可以使用我提供的链接从单独的C#WinForm的文档级外接程序中调用方法。 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? 为什么要构建一个与ReportGenerator加载项项目分开的ReportRunner Form项目? 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. 就像我在下面说的,您可以创建一个包含2个项目的VS解决方案-一个是文档级加载项,另一个是WinForm,您可以简单地从与该加载项关联的Ribbon中调用WinForm。


I assume that you're asking how to call a function from a Word Addin from a Winform? 我假设您正在问如何从Winform的Word Addin中调用函数? I recently explained how to do this here: How to call a VSTO AddIn method from a separate C# project? 我最近在这里解释了如何执行此操作: 如何从单独的C#项目中调用VSTO AddIn方法?

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: 话虽如此,我不建议您这样做,因为您可以将WinForm和Addin打包在一起,然后使用功能区将其打开:

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
            Form1 aForm = new Form1();
            aForm.Show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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