2

I am trying to open & write to existing word document inside a winforms application. Below is the code for the same.

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

This is working fine. But the issue I am facing is - When the user opens any word document from outside my application it gets opened under the same "wordApp" object that I have created above. So when I close my document with the below code, it closes the outside document & vice versa . Any suggestions ??

            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;

            }
user435293
  • 53
  • 5

0 Answers0