Consulting

Results 1 to 8 of 8

Thread: Solved: VB6 a WORD Kill

  1. #1
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    832
    Location

    Solved: VB6 a WORD Kill

    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
    [vba]
    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
    [/vba]

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Try
    [VBA]Objwordapp.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    Objwordapp.Quit
    [/VBA]

  3. #3
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    832
    Location
    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...
    [VBA]
    Objwordapp.ActiveDocument.Close SaveChanges:=0

    [/VBA]
    Any other suggestions? Dave

  4. #4
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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.

    [VBA]
    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
    [/VBA]

    [VBA]
    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

    [/VBA]

  5. #5
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    832
    Location
    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

  6. #6
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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...

  7. #7
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    832
    Location
    Found an API here that is the same as using cntrl-alt-del for the WORD application. http://www.access-programmers.co.uk/...d.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
    [VBA]
    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

    [/VBA]

  8. #8
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    832
    Location
    After learning lots about VB6, Get Object and error 429 (good info link here: http://visualbasic.freetutes.com/lea...sson10/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
    [VBA]
    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
    [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •