VSTO 以编程方式添加基于现有工作表的新工作表

发布于 2024-08-08 05:37:47 字数 641 浏览 3 评论 0原文

我目前有一个 C# 工作簿级 Office 2007 插件,它具有工作表在设计时添加,我已将方法和属性添加到 Worksheet 派生类。

我希望能够以编程方式创建一个新工作表,自动克隆继承或以其他方式拥有此现有 Worksheet 类的方法和事件处理程序。

如果可以实现这一点,那么有人可以概述一下如何实现这一点吗?尽管任何 .NET 代码都可以接受,但最好使用 C# 演示代码。

谢谢您的宝贵时间。

I currently have a C# Workbook-level Office 2007 Addin that has a Worksheet added at design-time where I have added methods and properties to the Worksheet derived class.

I want to be able to programatically make a new worksheet that clones, inherits or otherwise possesses the methods and event handlers of this existing Worksheet class automatically.

If this can be achieved then please could someone outline how this could be achieved? C# demo code would be preferably although any .NET code would be acceptable.

Thank-you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-08-15 05:37:47

好的,问得好,我会密切关注这里发生的事情。

几个月前,我遇到了类似的问题,需要在动态创建的工作表上创建代码和事件。然而,为了能够做到这一点,我最终在工作表中创建了 VBA 脚本对象。

所以过程是:

  1. 用户单击 Excel 中的按钮
  2. C# 过程创建新工作表
  3. C# 过程在工作表上创建 VBA 对象并创建事件等

     oBook = objExcel.ActiveWorkbook;

    oModule = oBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);

    string sCode = "sub myVBASub()\r\n"+
            "msgbox("Hello")\r\n"+
            "End Sub\r\n";

    oModule.CodeModule.AddFromString(sCode);

Ok, good question, and I'll be keeping an eye on what comes up on here.

A few months ago, I had a similar problem, and needed to create code and events on dynamically created sheets. To be able to do that, however, I ended up creating VBA script objects within the sheet.

So the process was:

  1. User Clicks button in Excel
  2. C# process creates new Sheet
  3. C# process creates VBA object on sheet and creates events, etc

     oBook = objExcel.ActiveWorkbook;

    oModule = oBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);

    string sCode = "sub myVBASub()\r\n"+
            "msgbox("Hello")\r\n"+
            "End Sub\r\n";

    oModule.CodeModule.AddFromString(sCode);

若沐 2024-08-15 05:37:47

我现在得出的结论是,按照我所寻找的那样在现有状态下复制这些类实际上是不可行的。

根据主机项和主机控件概述:在文档级插件中,主机项不能以编程方式创建,只能在设计时创建。主机项的对编程限制的进一步说明中强化了这一点和主机控件,特别是对于文档级插件。

I've now come to the conclusion that it's not actually feasible to duplicate these classes in their existing state as I was looking for.

According to the Host Items and Host Controls Overview: in Document Level Addins, Host Items cannot be created programmatically but only at design time. This is reinforced in the further explanation of the programmatic limitations of Host items and host controls, particularly for document level addins.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文