• Closing WINWORD.EXE Within VB6 Program (VB6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Closing WINWORD.EXE Within VB6 Program (VB6)

    Author
    Topic
    #414959

    I utilize VB6 to open and save many Microsoft Word XP documents each day. I open Word with the following code:

    Dim ObjWord As Object

    Set ObjWord = CreateObject(“Word.Basic”)
    With ObjWord

    I close the program with:

    ExitHandler:
    On Error Resume Next
    Set ObjWord = Nothing
    ObjWord.Quit
    Set ObjWord = Nothing
    Exit Sub

    I have noticed when I open Task Manager that there are numerous instances of WINWORD.EXE that are present. Is there anything I can do to completely close the WINWORD.EXE files when I close the VB6 program? As always, thank you!

    Bob Nichols

    Viewing 0 reply threads
    Author
    Replies
    • #924220

      You’re setting objWord = Nothing and THEN you’re issuing objWord.Quit, and then another objWord = Nothing. Take out the first one. You can’t issue a quit command when you’ve already destroyed the object reference.

      • #924254

        Thank you Charlotte! I made the suggested change, i.e deleted the first instance of “Set ObjWord = Nothing”, but the Task Manager still contained a WINWORD.EXE. Upon repeating the procedure, I got yet another WINWORD.EXE. Am I missing something? I am assuming it is not a good thing to have a dozen instances of WINWORD.EXE running at the same time. I would greatly appreciate any further guidance. Thank you!

        • #924266

          No, it isn’t a good thing. Did you check to make sure all instances of Winword were ended before you tried it again? From only the snippet of code you posted, I don’t think it’s possible to determine why Winword is staying open. Can you post more of the automation code so that someone might spot the problem?

          • #924268

            I will attach a Microsoft Word Document which contains all code set forth in the Command Button which utilizes WordXP. In order to make it a little more readable I have paginated it in Legal Size and in Landscape mode. Even then some lines of code have spilled over to a second line. I have highlighted in “red” some areas of interest. However, I obviously do not seem to know which lines of code are important. Please note that this command opens more than one (1) Word Template. Again, your help is greatly appreciated! Thank you!

            Bob Nichols

            • #924277

              The inconsistent indentation makes it too hard for me to find out what your code does.

              I would consider using Word VBA instead of WordBasic. Although WordBasic is still available for compatibility, VBA has been the standard programming language for Word since Word 97. You can use code like this to activate or start Word:

              Dim wrdApp As Word.Application
              Dim blnStartWord As Boolean

              On Error Resume Next
              ‘ See if Word is already active
              Set wrdApp = GetObject(, “Word.Application”)
              If wrdApp Is Nothing Then
              ‘ No, so start a new instance
              Set wrdApp = CreateObject(“Word.Application”)
              If wrdApp Is Nothing Then
              ‘ Failed
              MsgBox “Cannot activate Word.”
              Exit Sub
              End If
              blnStartWord = True
              End If
              On Error GoTo 0 ‘ or to an error handler

              The blnStartWord flag lets you keep track of whether you started a new instance of the Word application.

    Viewing 0 reply threads
    Reply To: Closing WINWORD.EXE Within VB6 Program (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: