title: Process used vb.net how to close
body: <pre><code> 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 &amp; "\" &amp; Date.Now.Ticks.ToString &amp; 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 &amp;= " 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 &gt; 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
  </code></pre>
  
  <p>After that i call the function to write to database</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>i Got the error the file is used by another process . Even if i try to kill the process it still wont solve the problem. </p>
  
  <p>Where am i making mistake with this ?</p>
  
  <p>Edit> Button click call this</p>
  
  <pre><code>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
  </code></pre>