2

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

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
4

2 回答 2

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 程序的文件)时,您可以找到可能触发错误的临时副本(以“~$...”开头)打开时(因此不允许物体释放部分进入画面);另外,一般来说,打开文件时可能会出现问题;这就是解释我添加的新 try...catch 以及为什么我将发布部分放在它之后的原因。

于 2013-09-10T07:45:42.190 回答
0

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

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

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

我将把它放在最后的“End If”之后。

希望对你有帮助

于 2013-09-10T07:25:06.180 回答