none
Word does not release file fast enough after being closed RRS feed

  • Question

  • After using a Word object with COM interop and closing it, the file I opened with Word remains locked by the Word process for a short while before it is released. This means I cannot delete the file after closing Word. I don't want to use workarounds like Thread.Sleep  or a loop with TryCatch to wait until I can delete the file, but I don't know what else to do.

    I looked for an event to tell me when Word finished closing, but there are only closing and beforeclosing events, which are both fired too early.

    Here is my code (simplified):

    objWord.Visible = False objDocument = objWord.Documents.Add(Filepath, , 0) objDocument.ActiveWindow.View.Type = 3 'do Word stuff

    '... objDocument.SaveAs(PdfPath, 17) objDocument.Close(0) 'Send PDF as Fax '... Finally objWord.Visible = False objWord.Quit() objWord = Nothing objDocument = Nothing objRange = Nothing GC.Collect() GC.WaitForPendingFinalizers() GC.Collect() GC.WaitForPendingFinalizers() ' System.Threading.Thread.Sleep(50) If IO.File.Exists(Filepath) Then .DeleteFile(Filepath)

    If I remove the Thread.Sleep I get an exception that the file is in use. If I comment it in, everything works fine.

    Monday, June 20, 2016 11:29 AM

Answers

All replies

  • Hello Mongan,

    I'd recommend starting from releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release a Word object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article. For example:

    objDocument = objWord.Documents.Add(Filepath, , 0)
    

    The Documents property of the Application class returns an instance of the corresponding class which should be released after.


    [custom.development]

    • Marked as answer by Mongan Tuesday, June 21, 2016 6:54 AM
    Monday, June 20, 2016 2:04 PM
  • Hi Mongan,

    please check the suggestion given by the Eugene  Astafiev.

    please check it and let us know whether it worked or not.

    if it worked then I would recommend you to mark the suggestion given by the Eugene  Astafiev as an Answer.

    if it not worked for you then let us know so that we can try to give you further suggestions.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 21, 2016 1:09 AM
    Moderator
  • Thanks, I've just tested it and it did the trick.

    The answer from Govert van Drimmelen in this thread suggested you shouldn't call finalreleasecomobject. After doing some search on my own, I found all the information on the internet to be wildly contradicting and just went on using the garbage collector only. I updated a complicated class that uses com interop with Excel once and using finalreleasecomobject was pretty much a nightmare, so  I was hoping the GC would suffice :(

    Tuesday, June 21, 2016 7:01 AM