vb.net中winword.exe的多个实例

时间:2018-10-01 09:20:15

标签: vb.net ms-word office-interop

我有一个vb.net应用程序,它逐个打开word 2016文档,并使用以下代码

   Public oWA As Microsoft.Office.Interop.Word.Application
   Public oDoc As Microsoft.Office.Interop.Word.Document
   oWA = New Microsoft.Office.Interop.Word.Application
   --below code line opens multiple word document one by one
  oDoc = oWA.Documents.Open(fileName, objMissing, objMissing, objMissing, 
   objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, 
    objMissing, objMissing, objMissing, objMissing, objMissing, objMissing)

问题是有时应用程序被挂起并停止处理Word文件。然后,我在任务管理器->进程中看到了Winword.exe的两个实例。当我杀死内存不足的那个时,应用程序会自动启动。这两个实例是如何生成的。我可以预防吗?为了解决这个问题,我创建了一个函数来杀死winword.exe实例,但是它不起作用。

Private Function KillMultipleWord()

    Dim currentProcess As Process()
    currentProcess = System.Diagnostics.Process.GetProcessesByName("winword")
    If currentProcess.Length > 1 Then
        Dim totalBytesOfMemoryUsed As Long = 0
        Dim cmptotalBytesOfMemoryUsed As Long = 0
        Dim cmpProc As System.Diagnostics.Process = Nothing
        For Each proc As System.Diagnostics.Process In currentProcess
            totalBytesOfMemoryUsed = proc.WorkingSet64
            If cmptotalBytesOfMemoryUsed < totalBytesOfMemoryUsed Then
                If cmpProc IsNot Nothing Then
                    cmpProc.Kill()
                End If
                cmpProc = proc
                cmptotalBytesOfMemoryUsed = totalBytesOfMemoryUsed
            Else
                proc.Kill()
            End If
        Next
        cmpProc = Nothing
    End If
    KillMultipleWord = Nothing
End Function

1 个答案:

答案 0 :(得分:1)

您需要使用

oDoc.Close()

在每个文档之后,

oWA.Quit()

完成后。