Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clean close of Word 1

Status
Not open for further replies.

AOLBoy

IS-IT--Management
May 7, 2002
68
GB
Dear All,
I am trying to create word documents from my VB6 application but I am having trouble trying to close MS Word. The following code prints and saves the document OK but leaves an instance of WINWORD.EXE in my task list. Each time I execute the code another instance is added to the list.

Public Function BuildDoc(UniqueID As String)
Dim cnx As New ADODB.Connection
Dim RS1 As ADODB.Record
Dim savfilename As String
'
Set WA = New Word.Application
WA.Visible = False
On Error GoTo DocError
'
savfilename = "c:\Filename" & UniqueID & ".doc"
'
Documents.Add
'
Selection.Font.Size = 14
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="Details for file " & UniqueID
Selection.TypeParagraph
Selection.TypeParagraph
'
ActiveDocument.PrintOut
'
ActiveDocument.SaveAs (savfilename)
'
ActiveDocument.Close
'
Set WA = Nothing
Exit Function
DocError:
MsgBox ("Document Error - " & Err.Number)
Exit Function
Set WA = Nothing
End Function

Can anybody help?
 
It is not killing the instance because it will be printing at that time.

Try placing "Doevents" after printout.

Or
initiate a timer after the printout statement and kill the instance in timer event as follows


Private Sub TimerPrint_Timer()
On Error Resume Next
WA.Application.Quit
If Err.Number = 0 Then
TimerPrint.Enabled = False
End If
End Sub
 
I use:

Do While WA.Application.BackgroundPrintingStatus > 0
DoEvents
Loop



®od
 
I use this sub to kill automation servers that don't respond to the Quit method. Quit sometimes destroys the application window but leaves the process running. You can use the FindWindow API to get the handle to the Word Application window before calling Quit then calling the sub.
Code:
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long

Public Const PROCESS_ALL_ACCESS = &H1F0FFF
'_______________________________
Public Sub sKillServer(Byval hWnd As Long)

  '--- Kills an OLE server that doesn't respond to the Quit method

  '--- Parameter
  '    hWnd: the OLE server's application window handle

  Dim lngRtn As Long
  Dim lngProc As Long
  Dim lngProcID As Long

  lngRtn = GetWindowThreadProcessId(hWnd, lngProcID)
  lngProc = OpenProcess(PROCESS_ALL_ACCESS, Clng(0), lngProcID)
  lngRtn = TerminateProcess(lngProc, Clng(0))

End Sub
Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top