Log in

View Full Version : Solved: VB6 a WORD Kill



Dave
12-19-2009, 11:34 PM
This works for VBA. Doesn't seem to want to work for VB6. From VB6 just trying to Quit the WORD application if it is running (it's part of an error routine). I'm assuming that this is some syntaxical thing if anyone can help. Dave
edit: Quit is better than Kill

Dim Objwordapp As Object
On Error Resume Next
Set Objwordapp = GetObject(, "word.application") 'didn't work
Set Objwordapp = GetObject("WORD.APPLICATION") 'didn't work
If Err.Number = 0 Then
Objwordapp.Quit 'didn't work
With Objwordapp
.Application.Quit
End With
End If
On Error GoTo 0
Set Objwordapp = Nothing

Tommy
12-20-2009, 07:58 AM
Try
Objwordapp.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Objwordapp.Quit

Dave
12-20-2009, 12:37 PM
Thanks Tommy. I trialled a few variations but no luck. Run time error -2147221020 (800401e4) automation error. Invalid syntax. It wouldn't compile without the Word constant...

Objwordapp.ActiveDocument.Close SaveChanges:=0


Any other suggestions? Dave

Tommy
12-20-2009, 01:51 PM
Did you try to iterate through the documents collection, closing each one?

Normally when I have trouble closing Word is when I threw an error, left a document open, but the biggest one is my word app is invisible and word has a dialog box displayed that you can't see. So as a trouble shooting hint make the word app visible to see if that can at least get something going.

I found this while I was looking at how I handled it, the AGAIN line was a where I either did what was required or told the end user what had happened like word wasn't installed, documents weren't found. Sometimes word is busy and can't react or respond until it is through. This means your request to close or quit will be ignored.


Public WordDoc As Word.Document
Public wrd As Word.Application
On Error Resume Next
Set wrd = Nothing
Set WordDoc = Nothing
Set wrd = New Word.Application
If Err.Number <> 429 And Err.Number <> 0 Then
Set WordDoc = wrd.ActiveDocument
Call KillWrd
If Err.Number = 91 Then
wrd.Quit
Set wrd = Nothing
Set WordDoc = Nothing
End If
Err.Clear
On Error GoTo 0
Set wrd = New Word.Application
End If
If Err.Number = 429 Or Err.Number = 462 Then
Err.Clear
On Error GoTo AGAIN
Set wrd = New Word.Application
End If



Public Sub KillWrd()
On Error Resume Next
wrd.Options.CheckGrammarAsYouType = hldCGAYT
wrd.Options.CheckGrammarWithSpelling = hldCGWS
wrd.Options.CheckSpellingAsYouType = hldCSAYT
WordDoc.Close SaveChanges:=wdDoNotSaveChanges
wrd.Quit
Set wrd = Nothing
Set WordDoc = Nothing
Err.Clear
End Sub

Dave
12-21-2009, 12:05 AM
Thanks again Tommy. Just to be a bit more clear, I'm looking for VB6 code to quit the Word application which may or may not be running. My code craps out when the Word application isn't running. I haven't got to the point of seeing if it will actually quit the Word application. I don't have a VB6 Word reference set. I was opening Word from XL which opened from VB6. So XL may crash and leave Word open. With the VB6 Word reference set it still doesn't seem to do what I want. I'll spent some more time on it and report back. I don't really understand your code with all that adding New Word.Applications. Dave

Tommy
12-21-2009, 06:12 AM
I understand that you are looking for VB6 code. That is what I posted. It was started in VB5 with Word 97 but is has been upgraded.

What that should br doing id gaining a reference to an existing word instance and quiting it, you should be able to get the reference and work with it. I was opening different documents...

Dave
12-22-2009, 02:50 PM
Found an API here that is the same as using cntrl-alt-del for the WORD application. http://www.access-programmers.co.uk/forums/showthread.php?t=139590
It works, but unfortunately, WORD thinks that it should be able to recover the documents. Tommy... maybe I'm not using your code correctly but it doesn't seem to do anything whether VB6 has a reference to WORD or not. This works without a VB6 WORD reference but only if the WORD application is running. If not, it throws an error 429. I don't get it and I haven't been able to google up a solution? Dave

Dim Objwordapp As Object
On Error Resume Next
Set Objwordapp = GetObject(, "word.application")
If Err.Number = 0 Then
Objwordapp.Quit
End If
Set Objwordapp = Nothing
On Error GoTo 0

Dave
12-22-2009, 07:39 PM
After learning lots about VB6, Get Object and error 429 (good info link here: http://visualbasic.freetutes.com/learn-vb6-advanced/lesson10/p7.html ) It occurred to me that the activex component can't create the object error (caused when using GetObject and no application is running) was telling me that my form (and therefore it's code) couldn't create the object/handle the error of not creating the object. I moved the code to a Module sub and called it from the form code and surprise it works (Both when a WORD application is not running or running, without any document recovery bs, and without a WORD reference). Seems all is well other than this solution wasn't Googleable (if that's a word). Thanls Tommy for your time and interest. Have a great Holiday Season! Dave
ps. Here's the final module code to quit Word from VB6. Just Call EndWord

Public Sub EndWord()
Dim Objwordapp As Object
On Error Resume Next
Set Objwordapp = GetObject(, "word.application")
If Err.Number = 0 Then
Objwordapp.Quit
End If
Set Objwordapp = Nothing
On Error GoTo 0
End Sub