DocumentOpen Word 事件不起作用,如何在打开 Word 文件时运行计时器

问题描述

文件打开时,我将其上传到服务器,但是当文件打开时 Application_DocumentOpen 方法不起作用

如何在文件打开时每 30 秒运行一次我的上传方法

  void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
 {
            System.Timers.Timer timer = new System.Timers.Timer(30000);
            timer.Enabled = true;
            
            
            timer.Elapsed += (sender,e) => 
            DocumentEditingSendToServer(Doc.Application.ActiveDocument);
}

解决方法

您需要创建一个 timer_tick 方法并每 30 秒从该方法上传一次。

像这样:

Timer Timer1 = new Timer();

void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
 {
            InitializeTimer();
}

void InitializeTimer()
{
     Timer1.Interval = 30000;
     Timer1.Tick += new EventHandler(Timer1_Tick);
}

private void Timer1_Tick (Object sender,EventArgs e)
{
     DocumentEditingSendToServer(Doc.Application.ActiveDocument);
}