简体   繁体   English

Office DocumentBeforeSave事件仅在绑定后几秒钟内起作用

[英]office DocumentBeforeSave events only work after bind in few seconds

I'm working on a feature which is to create a backup when a open word saved each times. 我正在开发一项功能,该功能是在每次保存一个打开的单词时创建备份。 I'm using the blow code to hooking into word process and bind events to it, the word is opened by process. 我正在使用打击代码来挂接到单词流程并将事件绑定到单词流程,单词是由流程打开的。

officeApplication = (Application)Marshal.GetActiveObject("Word.Application").
officeApplication.DocumentBeforeSave += new ApplicationEvents4_DocumentBeforeSaveEventHandler(App_BeforeSaveDocument);

And in App_BeforeSaveDocument I did my work. App_BeforeSaveDocument我做了工作。

I get officeApplication right, and bind events were fine, when I click save in word, the events triggered perfectly. 我正确使用了officeApplication ,并且绑定事件很好,当我单击“保存”时,事件触发得很好。 The problem is, a few seconds(may be 30s) after, the events will not fire anymore, no matter click save or save us or close document. 问题是,几秒钟后(可能是30秒),无论单击“保存”或“保存我们”还是关闭文档,事件都将不再触发。

Is there any suggestions? 有什么建议吗?

After a lot of researching, I still can't find the reason. 经过大量研究,我仍然找不到原因。 And I decide to use a trick to approach it. 而且我决定使用一种技巧来实现它。

First, open a thread in the binding event: 首先,在绑定事件中打开一个线程:

static void App_BeforeSaveDocument(Microsoft.Office.Interop.Word.Document document, ref bool saveAsUI, ref bool cancel)
            {
                if (th != null)
                    th.Abort();
                th = new Thread(backupOnSave);
                th.IsBackground = true;
                th.Start(document);
            }

Then do an infinity loop in the thread: 然后在线程中进行无限循环:

internal static void backupOnSave(object obj)
        {
            try
            {
                Application app = obj as Application;
                if (app == null || app.ActiveDocument == null)
                {
                    return;
                }
                Microsoft.Office.Interop.Word.Document document = app.ActiveDocument;
                if (!tempData.ContainsKey(document.FullName))
                    return;
                var loopTicks = 2000;

                while (true)
                {
                    Thread.Sleep(loopTicks);
                    if (document.Saved)
                    {
                        if (!tempData.ContainsKey(document.FullName))
                            break;
                        var p = tempData[document.FullName];
                        var f = new FileInfo(p.FileFullName);

                        if (f.LastWriteTime != p.LastWriteTime)//changed, should create new backup
                        {
                            BackupFile(p, f);
                            p.LastWriteTime = f.LastWriteTime;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.write(ex);
            }
        }

And it works fine. 而且效果很好。 Don't remember to abort the thread when the document closed or exception happen. 当文档关闭或发生异常时,不要记得中止线程。

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

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