0

i created a word doc in vb but the problem is each time my button is clicked it opens a new doc. i have tried the following code to terminate it after it fulfilled its purpose but nothing seems to work and i have search multiple sites but found no answer that helped i used interop

my attempts to close the background winword.exe (objDoc is the the word doc )

      objDoc.Close()
        objDoc.Application.Quit()
        objDoc.Application.DDETerminateAll()
        GC.Collect()
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objDoc) 

and yes i did look at the post on this site about closing interop word applications but its fails so i am open to any new ideas

Wolf
  • 170
  • 1
  • 4
  • 19
  • this does not work http://stackoverflow.com/questions/6777422/disposing-of-microsoft-office-interop-word-application i tried it ,dont know what is wrong but my pc may be possessed or in love with word – Wolf Jan 30 '14 at 07:35

3 Answers3

1

you are closing the objdoc instead of objWord

        objDoc.Close()
        objWord.Application.Quit(False)
        objWord = Nothing
0

You could just kill the process? Not 100% the best way to do it, but at least you'll be sure that word is being closed.

 Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("winword")

 For Each p As Process In pProcess
     p.Kill()
 Next

Some code i'm personnaly using to work with Word ( occassion : wordcompare on the server)

  • Doc = object of the type Interop.Word.Document
  • Dim _myApp As Interop.Word.Application = New Interop.Word.Application()
  • Dim vk_false As Object = false
  • Dim vk_missing As Object = System.Reflection.Missing.Value

    Marshal.ReleaseComObject(doc)
    _myApp.Quit(vk_false, vk_missing, vk_missing);
    Marshal.FinalReleaseComObject(_myApp);
    
User999999
  • 2,500
  • 7
  • 37
  • 63
  • I thought i had some alternative code aswell. Will try to find it – User999999 Jan 30 '14 at 07:54
  • thank you and i tried the process kill it failed this is a problem because i had 178 winword.exe open yesterday it will cause major problems with the program if not fixed – Wolf Jan 30 '14 at 07:55
  • what error are you getting from the code i gave you? – User999999 Jan 30 '14 at 07:57
  • no errors but the process still opens and does not close but it was a nice idea i should have thought of it.If nothing works i will have to change my code completely which will be a pain in the rear end – Wolf Jan 30 '14 at 07:59
  • Do you have written some extra plugin in Word? Or is the word doc being opened from somewhere else? I find it hard to believe that the first piece of code isn't working. – User999999 Jan 30 '14 at 08:10
  • the word opens in the background ,information is added and then converted to a pdf and saved to share-point ,if i open task-manager and look under process i find a winWord.exe for everytime i clicked the save button i am using a callback and all of the above methods have been tried without success it should have worked : Dim objWord As New Word.Application() Dim objDoc = objWord.Documents.Add() – Wolf Jan 30 '14 at 08:12
  • the only other reason may be my layout it needs to closed after it is saved not before so i am going to go look and see what i can do Thank you Svranken il give you the answer because you tried to help me – Wolf Jan 30 '14 at 08:22
0

I had similar problem, and decided to kill process after I close Word, because otherwise if I run my program multiple times(10-15), I had 10-15 WINWORD.EXE processes in Task Manager. For this purpose I call command prompt after I release objects and kill process via it with this line Shell("Taskkill /IM WINWORD.exe /F").

It looked like

        try 
.
.
.
        objDoc.Close()
                objDoc.Application.Quit()
                objDoc.Application.DDETerminateAll()
                GC.Collect()
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objDoc)

        Catch ex As Exception
            MsgBox("Error: " & ex.ToString, MsgBoxStyle.Critical, "Error!")
        End Try      

       Shell("Taskkill /IM WINWORD.exe /F")
user2771704
  • 5,994
  • 6
  • 37
  • 38
  • thank you i tried it but still nothing works, i am beginning to think my pc is possessed by a demon ,going to burn the thing ...and roast marshmallows over it ... i appreciate the help my layout might just be wrong i am trying to figure out exactly where i should but the kill it must be after the save else il kill the doc with my information unsaved – Wolf Jan 30 '14 at 08:48
  • Strange, this worked perfect for me. Yes, you must put this code in the end, or call after you finish your work or you lose your data. Good luck with solution. – user2771704 Jan 30 '14 at 08:55
  • thanks a lot i placed it at the end from the start but nothing works it is strange indeed all the suggested methods and those i tried myself should have worked i dont know what could be the cause of this but it is a fun challenge so il keep trying thank you – Wolf Jan 30 '14 at 08:57