关闭Word应用程序,vb.net

时间:2013-09-10 07:16:54

标签: vb.net ms-word

我正在尝试在word文档中搜索,使用vb.net它有效但我似乎无法在搜索后关闭文件,这里是我使用的代码 如何在搜索后关闭应用程序?

Dim oWord As Word.Application = Nothing
    Dim oDocs As Word.Documents = Nothing
    Dim oDoc As Word.Document = Nothing
    Dim folderDlg As New FolderBrowserDialog
    folderDlg.ShowNewFolderButton = True
    If (folderDlg.ShowDialog() = DialogResult.OK) Then
        Dim root As Environment.SpecialFolder = folderDlg.RootFolder
    End If
    Dim l_Dir As IO.DirectoryInfo
    Dim fldpath As String = folderDlg.SelectedPath
    If IO.Directory.Exists(fldpath) Then
        l_Dir = New IO.DirectoryInfo(fldpath)

        For Each l_File In Directory.GetFiles(fldpath, "*.docx")


            Dim searchFor As String = TextBox1.Text


            oWord = New Word.Application()
            oWord.Visible = False

            oDocs = oWord.Documents
            oDoc = oDocs.Open(l_File, False)
            oDoc.Content.Find.ClearFormatting()

            Dim findText As String = searchFor

            Try
                If oDoc.Content.Find.Execute(findText) = True Then
                    MessageBox.Show("OK.")

                    oWord.NormalTemplate.Saved = True
                    oWord.ActiveDocument.Close(False)
                    oDoc.Close()
                    oWord.Quit()

                    If Not oDoc Is Nothing Then
                        Marshal.FinalReleaseComObject(oDoc)
                        oDoc = Nothing
                    End If
                    If Not oDocs Is Nothing Then
                        Marshal.FinalReleaseComObject(oDocs)
                        oDocs = Nothing
                    End If
                    If Not oWord Is Nothing Then
                        Marshal.FinalReleaseComObject(oWord)
                        oWord = Nothing
                    End If

                Else
                    MessageBox.Show("No.")
                End If

            Catch ex As Exception

            End Try
            ComboBox1.Items.Add(l_File)
        Next

    End If

2 个答案:

答案 0 :(得分:3)

首先要记住的是,在Word中“释放对象”并不像在Excel中那么困难,因此你(不必要地)使事情过于复杂。在任何情况下,您都应该打算不过度声明变量(oDocs的确切点是什么?)。而且,最后,当出现问题时,您应该始终执行逐步执行以找出可能发生的情况(您仅针对“OK”情况应用“对象释放”,而不是在任何情况下:结果是“否”,对象也必须被释放。)

这里有一个更正的代码,可以解释上述所有问题:

Dim oWord As Word.Application = Nothing
Dim oDoc As Word.Document = Nothing
Dim folderDlg As New FolderBrowserDialog
folderDlg.ShowNewFolderButton = True
If (folderDlg.ShowDialog() = DialogResult.OK) Then
    Dim root As Environment.SpecialFolder = folderDlg.RootFolder
End If
Dim l_Dir As IO.DirectoryInfo
Dim fldpath As String = folderDlg.SelectedPath
If IO.Directory.Exists(fldpath) Then
    l_Dir = New IO.DirectoryInfo(fldpath)

    For Each l_File In Directory.GetFiles(fldpath, "*.docx")

        Dim searchFor As String = TextBox1.Text

        oWord = New Word.Application()
        oWord.Visible = False

        Try
            oDoc = oWord.Documents.Open(l_File, False)
            oDoc.Content.Find.ClearFormatting()

            Dim findText As String = searchFor

            Try
                If oDoc.Content.Find.Execute(findText) = True Then
                    MessageBox.Show("OK.")

                Else
                    MessageBox.Show("No.")
                End If

            Catch ex As Exception

            End Try

            oWord.NormalTemplate.Saved = True

            ComboBox1.Items.Add(l_File)
        Catch ex As Exception

        End Try

        oDoc = Nothing
        oWord.Quit()
        oWord = Nothing

    Next

End If

注意:请注意,在遍历文件夹中的所有Word文件(以及通常来自任何MS Office程序的文件)时,您可以找到临时副本(以“〜$ ...”开头)打开时触发错误(因此不允许对象释放部分进入画面);一般来说,打开文件时可能会出错;这就是新尝试的解释...我添加的捕获以及为什么我将释放部分放在它之后。

答案 1 :(得分:0)

我不知道Vb.net,但在F#中我使用了

System.Runtime.InteropServices.Marshal.ReleaseComObject xlApp |> ignore

在“.Quit()”之后,要结束这个过程,但你必须搜索,如何在xlApp的地方给出你的单词应用程序的名称

我会把它放在最后的“结束如果”之后。

我希望它对你有所帮助