在VSTO Excel加载项应用程序中管理ActiveWorkbook的最佳方法 [英] Best way to manage the ActiveWorkbook in a VSTO Excel Addin application

查看:47
本文介绍了在VSTO Excel加载项应用程序中管理ActiveWorkbook的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了相当密集的Excel插件(基于VTSO),该插件通过数据库查询创建内存中的数据表,然后将其绑定到ListObject.创建此ListObject时,还有很多其他功能(包括更新数据表中计算的事件处理程序以及弹出以显示有关单元格的其他信息的自定义任务窗格).

I've developed a rather intensive Excel Addin (VTSO based) that creates an in-memory datatable from database query, and then binds this to ListObject. There are a bunch of other functions available when this ListObject is created (including event handlers that update calculations in the datatable, and custom task panes that popout to show additional information about a cell).

尽管用户打开其他Excel工作簿或Excel的其他实例进行工作中的多任务时遇到问题,但Addin仍能很好地工作.

The Addin works well, although I'm getting issues when users open other Excel workbooks or additional instances of Excel to multi-task on work.

管理此特定应用程序状态的最佳方法是什么?用户一次只能使用该工作簿的一个副本,但是Excel的灵活性使其难以管理.

What's the best way to manage the state of this particular application? Users should only ever work on one copy of this workbook at a time, however the flexibility of Excel makes it difficult to manage.

其他人如何管理?我当时正在考虑在创建工作簿时创建一个GUID,然后在调用代码以确保活动工作簿是正确的工作簿时对此进行测试.还是有更好的方法?

How do others manage this? I was thinking of creating a GUID when a workbook is created, and then testing for this whenever code is called to make sure that the active workbook is the correct workbook. Or is there a better way?

推荐答案

我相信您不应该强迫用户仅使用一个工作簿.

I believe that you shouldn't force users to only work with a single workbook.

对于excel 2013及更高版本,您应该维护对所有打开的工作簿的引用(例如,在词典中).每次激活/停用新工作簿时,您都需要更新这些引用.您可以使用与唯一的顶级窗口句柄相对应的整数 Globals.ThisAddIn.Application.Hwnd 作为字典的键.您可以将每个键附加到与您的应用程序状态相对应的类上,每次触发工作簿激活/停用事件时,您都需要更新这些状态.

For excel 2013 and higher, you should maintain a reference to all workbooks that are open (in a dictionary for instance). Every time a new workbook is activate/deactivate you need to update those references. You can use the integer Globals.ThisAddIn.Application.Hwnd that correspond to the unique top level windows handle as a key for your dictionary. You can attach each key to a class that correspond to the state of your application, every times the workbook activate/deactivate event is fired you need to update those states.

换句话说,借助 Globals.ThisAddIn.Application.Hwnd ,您应该将应用程序的设置(数据库连接...)附加到工作簿,而无需使用GUID.

In other words, you should attach the setting of your application (database connection ...) to workbooks thanks to Globals.ThisAddIn.Application.Hwnd, no need to use a GUID.

另请参见此答案以供参考.

这篇关于在VSTO Excel加载项应用程序中管理ActiveWorkbook的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆