是否有AfterSave / AfterClose事件或有什么方法来伪造它们。 [英] Are there AfterSave/AfterClose events or is there some way to fake them.

查看:185
本文介绍了是否有AfterSave / AfterClose事件或有什么方法来伪造它们。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VSTO 2003应用程序加载项,我需要运行一些代码,但前提是我确定文档已保存,或已关闭并保存。不幸的是,似乎excel interop API只提供BeforeSave和BeforeClose事件,但不提供事后事件。我想用以下代码伪造它们


公共类WorkbookEventHandling 
{
public ExcelInterop.Workbook Workbook {get;组; }

public WorkbookEventHandling(ExcelInterop.Workbook工作簿)
{
工作簿=工作簿;
Workbook.BeforeClose + = new Microsoft.Office.Interop.Excel.WorkbookEvents_BeforeCloseEventHandler(Workbook_BeforeClose);
Workbook.Deactivate + = new Microsoft.Office.Interop.Excel.WorkbookEvents_DeactivateEventHandler(Workbook_Deactivate);
}

void Workbook_Deactivate()
{
if(closing)
{
//保存项目后关闭后b $ b}
}

bool closing = false;

void Workbook_BeforeClose(ref bool Cancel)
{
closing = true;
}
}


但是我担心另一个插件会将cancel设置为false并且我的代码会表现不正确,因为工作簿不会真正关闭下一次停用。以前有人遇到过类似的东西吗?知道解决方法吗?

解决方案

你好,

我对这个场景的建议是在Workbook BeforeClose事件中启动一个计时器,检查是否关闭实际上已从Application.Workbooks集合中删除了工作簿。
如果将其删除,则停止计时器。

如果您还面向用户单击"保存工作簿"提示下的"取消"按钮,则可以查看此链接中的想法:
http://j-walk.com/ss/excel/提示/ tip78.htm

感谢

I have a VSTO 2003 application add-in where I need to run some code, but only if I am certain that a document has been saved, or closed and saved.  Unfortunately it appears that excel interop API's only offer a BeforeSave and BeforeClose events, but not the after events.  I thought about faking them with the following code

public class WorkbookEventHandling
    {
        public ExcelInterop.Workbook Workbook { get; set; }

        public WorkbookEventHandling(ExcelInterop.Workbook workbook)
        {
            Workbook = workbook;
            Workbook.BeforeClose += new Microsoft.Office.Interop.Excel.WorkbookEvents_BeforeCloseEventHandler(Workbook_BeforeClose);
            Workbook.Deactivate += new Microsoft.Office.Interop.Excel.WorkbookEvents_DeactivateEventHandler(Workbook_Deactivate);
        }

        void Workbook_Deactivate()
        {
            if (closing)
            {
                //do the after closing, after save items
            }
        }

        bool closing = false;

        void Workbook_BeforeClose(ref bool Cancel)
        {
            closing = true;
        }
    }

but I am concerned that another addin will set cancel to false and my code will behave incorrectly as the workbook will not be really be closing on the next deactivate.  Has anyone faced something simlar before?  Know of a workaround?

解决方案

Hello,

My suggestion to this scenario is start a timer in Workbook BeforeClose event, check if the closing workbook is actually removed from Application.Workbooks collection.
If it is removed, then stop the timer.

If you also facing user click cancel button at Save Workbook prompt, you could take a look the idea in this link:
http://j-walk.com/ss/excel/tips/tip78.htm

Thanks


这篇关于是否有AfterSave / AfterClose事件或有什么方法来伪造它们。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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