none
c# winfom 调用 Microsoft.Office.Interop.Word 问题 RRS feed

  • 问题

  • 你好

    环境 VS2015   .net 4.0   

    程序集 Microsoft.Office.Interop.Word
        C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll

      如标题,我在按钮下自动生成WORD 文档

            //新建一个WORD对象
                        wordDoc2 = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                        //保存
                        object format = MSWord2.WdSaveFormat.wdFormatDocument;
                        wordDoc2.SaveAs(ref path, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                        //关闭word文档
                        wordDoc2.Close(ref Nothing, ref Nothing, ref Nothing);

                        //打开word文档         
                        wordDoc2 = wordApp.Documents.Open(ref path, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,

    中间代码有点长省略了

     wordApp.DocumentBeforeSave += new ApplicationEvents4_DocumentBeforeSaveEventHandler(wordApp_DocumentBeforeSave2);
           这边word生成结束

            private void wordApp_DocumentBeforeSave2(MSWord2.Document doc, ref bool SaveAsUI, ref bool Cancel)/*自主定义函数病程记录,word保存时将数据保存到数据库*/
            {

                        DataBase database = new DataBase();
                        OracleConnection conn1 = database.GetDbConnection();
                        OracleCommand cmd1 = new OracleCommand();
                        cmd1.Connection = conn1;
                        try
                        {
                            cmd1.CommandText = "update ys_zy_bl02 set dxqz='" + wordDoc2.Tables[1].Cell(2, 1).Range.Text + "' where BLBH='" + BLBH + "' and dxbh=14259 ";
                            cmd1.ExecuteNonQuery();
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show("错误" + ex);
                        }
                        finally
                        {
                            cmd1.Dispose();
                            conn1.Close();
                            conn1.Dispose();
                        }
                        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
            }
            

    遇到的问题是

    word文档生成后  有时候会出现保存的时候不触发 wordApp_DocumentBeforeSave2  规律还没有发现  有时候保存  有时候不可以保存  有时候开始可以保存  中间突然不能保存

    谢谢



    2018年9月29日 1:11

全部回复

  • 你好,

    由于不能重现,我也太不清楚是什么原因,不过你可以试试下面的方法。

    首先:在绑定事件中打开一个新的线程。

    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);
    }

    然后 在这个线程里做一个循环:

    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);
                }
            }


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年10月1日 5:43
    版主
  • 刚看到 感谢  我先试试  多测试下
    2018年10月7日 7:28
  • 还是会出现 保存的时候不触发 wordApp_DocumentBeforeSave 的情况 

    我在调试模式下  打了断点  跟不进去 点击保存按钮没有反应的。

      我修改了WORD里面的文字  直接点关闭  就直接关闭了  理论上不应该需要提示是否保存吗(WORD自带功能) ?

    是否和我 System.Windows.Forms.Application.DoEvents(); 这句话有关系。 

    感谢 

    2018年10月8日 5:46
  • 你好,

    试在你的方法里面记录一些日志,然后看看日志里面有没有一些,你可以使用log4net 来记录日志。 关于log4net 的用法,可以参考下面的博客.

    https://www.cnblogs.com/vichin/p/6022612.html

     

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年10月8日 6:06
    版主