2

我正在尝试在 winforms 应用程序中打开并写入现有的 word 文档。下面是相同的代码。

            object readOnly = false;
            object isVisible = true;
            object oMissing = System.Reflection.Missing.Value;
            object strDocPath = Environment.CurrentDirectory + @"\" + templateFileName;
            wordApp = new Microsoft.Office.Interop.Word.Application();

           //Set the AppId to uniquely identify word doc
            string AppId = "" + DateTime.Now.Ticks;
            this.wordApp.Application.Caption = AppId;
            this.wordApp.Visible = true;


            wordDoc = wordApp.Documents.Open(ref strDocPath, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing,
                                          ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible,
                                          ref oMissing, ref oMissing, ref oMissing, ref oMissing);

这工作正常。但我面临的问题是 - 当用户从我的应用程序外部打开任何 word 文档时,它会在我上面创建的同一个“wordApp”对象下打开。因此,当我使用以下代码关闭文档时,它会关闭外部文档,反之亦然。有什么建议么 ??

            if (wordDoc != null)
            {
                this.wordDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument);
                Marshal.FinalReleaseComObject(wordDoc);
                wordDoc = null;
            }

            if (wordApp != null)
            {                                 
                wordApp.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                wordApp = null;

            }
4

0 回答 0