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

vb.net中winword.exe的多个实例,vb.net,ms-word,office-interop,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 wo

我有一个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文件。然后我在TaskManager->Process中看到了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
私有函数KillMultipleWord()
Dim currentProcess作为进程()
currentProcess=System.Diagnostics.Process.GetProcessByName(“winword”)
如果currentProcess.Length>1,则
Dim totalBytesOfMemoryUsed,当Long=0时
Dim CMPTOTALBYTESOFMEMORY长=0时使用
Dim cmpProc As System.Diagnostics.Process=无
对于currentProcess中作为System.Diagnostics.Process的每个过程
totalBytesOfMemoryUsed=proc.WorkingSet64
如果cmptotalBytesOfMemoryUsed
您需要使用

oDoc.Close()
在每份文件之后,以及

oWA.Quit()
完成后。

您需要提供一个。您发布的代码片段不包含“循环”功能,因此我们无法确切看到问题所在。然而,最有可能的是,在循环的每次迭代中调用
New Word.Application
。您应该只在循环之前调用它一次,然后循环打开文档-所有这些都在Word.Application的同一个实例中。当你处理完一个文档后,一定要关闭并发布它。当您使用完Word.Application后,一定要正确地发布它。否则,您的代码将永远无法正确运行。