none
Application.Quit(false) not quitting Word instance RRS feed

  • Question

  • I'm calling this from inside a Word AddIn (COM, not VSTO). So that may be causing it to hang around.

    I'm trying to create an additional Word instance. The sole purpose of this code is to get the entire document into the clipboard (because Microsoft does not document the clipboard format for DOCX).

    The problem is upon completion, the 2nd instance of Word is still in memory. I need to kill that second instance.

    The following is my code (it implements IDisposable so the Close() is the cleanup). 

    		public ExWordDragDropWrapper(Application autoTagWordApp, byte[] docx, DragDropData ddData) : base(ddData)
    		{
    			// copy to a file
    			srcFile = Path.Combine(Path.GetTempPath(),
    							 "Windward_TempClipboard_" + Guid.NewGuid() + ".docx");
    			using (FileStream tempFileOutStream = File.Create(srcFile)) {
    				tempFileOutStream.Write(docx, 0, docx.Length);
    				tempFileOutStream.Close();
    			}
    
    			this.autoTagWordApp = autoTagWordApp;
    			autoTagDoc = autoTagWordApp.ActiveDocument;
    
    			wordApp = new Application();
    			wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
    			documents = wordApp.Documents;
    			
    			object file = srcFile;
    			object oMissing = System.Reflection.Missing.Value;
    			srcDoc = documents.Open(ref file, ref oMissing, true);
    			wordApp.NormalTemplate.Saved = true;
    
    			srcDoc.Activate();
    			activeWindow = srcDoc.ActiveWindow;
    			selection = activeWindow.Selection;
    			selection.WholeStory();
    			selection.Copy();
    			// data = Clipboard.GetDataObject();
    
    			srcDoc.Saved = true;
    			object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    			srcDoc.Close(ref saveChanges, ref oMissing, ref oMissing);
    			File.Delete(srcFile);
    		}
    
    		public override void Close()
    		{
    			base.Close();
    
    			// switch back to our app/document
    			autoTagWordApp?.Activate();
    			autoTagDoc?.Activate();
    			// clear because the created app is tied to any clipboard requests.
    			Clipboard.Clear();
    
    			autoTagWordApp = null;
    			autoTagDoc = null;
    
    			// first we quit
    			if (wordApp != null) {
    				object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    				object oMissing = System.Reflection.Missing.Value;
    				wordApp.Quit(ref saveChanges, ref oMissing, ref oMissing);
    			}
    
    			// then we release the objects.
    			if (selection != null)
    				Marshal.ReleaseComObject(selection);
    			if (activeWindow != null)
    				Marshal.ReleaseComObject(activeWindow);
    			if (srcDoc != null)
    				Marshal.ReleaseComObject(srcDoc);
    			if (documents != null)
    				Marshal.ReleaseComObject(documents);
    			if (wordApp != null)
    				Marshal.ReleaseComObject(wordApp);
    
    			// they need to be gone, gone, gone
    			wordApp = null;
    			documents = null;
    			srcDoc = null;
    			activeWindow = null;
    			selection = null;
    
    			// force the cleanup (yes twice)
    			GC.Collect();
    			GC.WaitForPendingFinalizers();
    			GC.Collect();
    			GC.WaitForPendingFinalizers();
    		}

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Sunday, March 19, 2017 5:17 PM

Answers

All replies

  • The above code works - except on one system. (On that system it appears to be the network layer is not releasing a resource.)

    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    • Marked as answer by DavidThi808 Monday, March 20, 2017 2:44 PM
    Monday, March 20, 2017 2:44 PM
  • Hello David,

    Thanks for sharing the solution here and good luck with your projects.

    Regards,

    Celeste


    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.

    Tuesday, March 21, 2017 1:45 AM
    Moderator