在winform中打开word文档

时间:2013-09-04 07:25:18

标签: c# winforms automation ms-word ms-office

我正在尝试打开&写入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;

            }

0 个答案:

没有答案