程序使用vb.net如何关闭

程序使用vb.net如何关闭,vb.net,Vb.net,之后,我调用函数写入数据库 Private Sub _LoadAndWrite() Dim objWdDoc As Object Dim objWord As Object objWord = CreateObject("Word.Application") objWdDoc = CreateObject("Word.Document") Dim regKey As RegistryKey Dim

之后,我调用函数写入数据库

 Private Sub _LoadAndWrite()
        Dim objWdDoc As Object
        Dim objWord As Object
        objWord = CreateObject("Word.Application")
        objWdDoc = CreateObject("Word.Document")

        Dim regKey As RegistryKey
        Dim path As String

        regKey = Registry.LocalMachine.OpenSubKey("Software\tDocManager", True)
        path = regKey.GetValue("DocPath", "")
        Dim DocPath As New System.IO.FileInfo(path)
        regKey.Close()

        Dim FileToCopy As String
        FileToCopy = DocPath.FullName
        NewCopy = DocPath.DirectoryName & "\" & Date.Now.Ticks.ToString & DocPath.Extension

        If System.IO.File.Exists(FileToCopy) = True Then
            System.IO.File.Copy(FileToCopy, NewCopy)
        End If

        objWdDoc = objWord.Documents.Add(NewCopy)
        objWord.Visible = True

        Dim sQ As String = ""
        sQ &= " SELECT ...."

        Dim _cmd As New SqlCommand
        With _cmd
            .CommandText = sQ
            .Connection = dbMain
        End With

        Dim _da As New SqlDataAdapter(_cmd)
        Dim _dt As New DataTable

        Try
            _da.Fill(_dt)
        Catch ex As Exception

        End Try

        If _dt.Rows.Count > 0 Then
            For Each dr In _dt.Rows
                With objWdDoc.Bookmarks

                    If .Exists("tt") = True Then
                        .item("tt").Range.Text = IIf(IsDBNull(dr("tt")), "", dr("tt"))
                    End If
                End With
            Next
        End If

End Sub
我得到一个错误文件被另一个进程使用。即使我试图终止进程,它仍然无法解决问题

我在哪里犯了这个错误

编辑>按钮单击调用此

  Private Sub WriteWordDoc(filename As String, data As Byte())
        Dim fs As New System.IO.FileStream(filename, IO.FileMode.Create)
        Dim bw As New System.IO.BinaryWriter(fs)
        bw.Write(data)
        bw.Close()
        fs.Close()
    End Sub

WriteWordDoc
中,您从哪里得到错误?是的,我忘了提到这个。在WriteWordDoc子文件的第一行中,第一段代码的意义是什么?它不调用
WriteWordDoc
那么我们应该从中收集什么呢?它有按钮点击事件的代码。线程从
objWord.Documents.Add(NewCopy)
更新,看起来Word可能仍在使用它。我看不到任何东西可以关闭、释放或处理它。
If MsgBox("Do you want to generate the document for current job ", MsgBoxStyle.YesNoCancel, "Word document") = MsgBoxResult.Yes Then
        _LoadAndWrite()
    End If

If MsgBox("Do you want to save document with changes ", MsgBoxStyle.YesNoCancel, "Word document") = MsgBoxResult.Yes Then
    _SaveDocument(NewCopy)
End If

   Public Sub _SaveDocument(ByVal doc2 As String)
        Dim file As String = doc2
        Dim doc() As Byte = ReadWordDoc(file)
End Sub