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
我收到了另一个进程使用该文件的错误。即使我试图杀死这个过程,它仍然不会解决问题。
我在哪里弄错了?
编辑>按钮单击调用此
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