problem in mail retrive word.docx with marge field from database

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi
when retrieve word document from database the problem is cant open the file
note: I used marge field when save the document in data


Dim cn As New SqlConnection<br/>
Dim ds As New DataSet<br/>
Dim sql As String<br/>
Dim bytearr As Byte()<br/>
sql = "Select Image From Documents "
Dim Config As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader<br/>
Dim strConn As String = CType(Config.GetValue("cn.ConnectionString", GetType(System.String)), String)<br/>
cn.ConnectionString = strConn<br/>
cn.Open()
<br/>
Dim da As New SqlDataAdapter(sql, cn)<br/>
If cn.State = ConnectionState.Closed Then<br/>
cn.Open()<br/>
End If
da.SelectCommand.Parameters.Add("@ID", SqlDbType.Int).Value = Me.cmbFiles.SelectedValue
da.Fill(ds)
bytearr = da.SelectCommand.ExecuteScalar<br/>
Dim mem As New MemoryStream(bytearr)<br/>
If System.IO.File.Exists(Environment.CurrentDirectory & "" & "File.docx") Then System.IO.File.Delete(Environment.CurrentDirectory & "" & "File.docx")<br/>
Dim f As FileStream = New FileStream(Environment.CurrentDirectory & "" & "File.docx", FileMode.Create)<br/>
f.Write(bytearr, 0, bytearr.Length)
<br/>
Dim wrdApp As New Word.Application<br/>
Dim wrdDoc As New Word.Document

wrdDoc = wrdApp.Documents.Open(Environment.CurrentDirectory & "" & "File.docx")

<br/>
If System.IO.File.Exists(Environment.CurrentDirectory & "" & "tt.txt") Then System.IO.File.Delete(Environment.CurrentDirectory & "" & "tt.txt")
wrdApp.Visible = True

ExportTableToText(dt, Environment.CurrentDirectory & "", "tt", "txt")<br/>
wrdDoc.MailMerge.OpenDataSource(Environment.CurrentDirectory & "" & "tt.txt")<br/>
wrdDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument<br/>
wrdDoc.MailMerge.Execute()<br/>
wrdDoc.Close(False)

View the full article
 
Back
Top