by KKLakhanpal (2 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 11th December 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Using Msword 8.0 Library to send data from MSSQLServer to a word file
API Declarations
Dim thisrange As Range
Dim wordapp As Word.Application
Dim rs As New ADODB.Recordset
dim cnn as new adodb.connection
cnn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=user1;Initial Catalog=pubs;Data Source=NTSERVER"
cnn.Open
rs.Open "select * from quessel order by marks", cnn, adOpenDynamic, adLockBatchOptimistic
If rs.BOF Then
MsgBox "No Record Exists,First Insert a Record", vbOKOnly, "Message"
Else
Set wordapp1 = CreateObject("Word.Application")
Set thisdoc1 = wordapp1.Documents.Add
Set thisrange1 = thisdoc1.Paragraphs(1).Range
thisrange1.Font.Size = 12
thisrange1.Font.Bold = False
thisrange1.ParagraphFormat.Alignment = wdAlignParagraphLeft
thisrange1.InsertAfter "Paper" & vbCrLf
thisrange1.InsertAfter "CLASS :-" & rs("class") & vbCrLf
thisrange1.InsertAfter "SUBJECT :-" & rs("subject") & vbCrLf
thisrange1.InsertAfter "CODE :-" & rs("code") & vbCrLf & vbCrLf & vbCrLf
thisrange1.InsertAfter "Time Allowed :- " & rs("Time") & Space(50) & "Maximum Marks :-" & rs("maxmarks") & vbCrLf & vbCrLf
thisrange1.InsertAfter wordinp & vbCrLf
wordinp1 = ""
coun1 = 1
rs.MoveFirst
While Not rs.EOF
wordinp1 = ""
If IsNull(rs(0)) Then
wordinp1 = wordinp1 + Space(10)
Else
wordinp1 = wordinp1 + Space(10) + rs(0)
End If
If IsNull(rs(1)) Then
wordinp1 = wordinp1 + Space(10)
Else
wordinp1 = wordinp1 + Space(10) + Str(rs(1))
End If
wordinp1 = Str(coun1) + wordinp1
thisrange1.InsertAfter wordinp1 & vbCrLf
coun1 = coun1 + 1
rs.MoveNext
Wend
End If
thisdoc1.SaveAs "c:\abc.doc"
wordapp1.Quit
MsgBox "Question Paper Report Saved" & vbCrLf & "May Need Formatting in Document", vbOKOnly, "Successfully Saved"
rs.Close
No comments have been posted about Using Msword 8.0 Library to send data from MSSQLServer to a word file. Why not be the first to post a comment about Using Msword 8.0 Library to send data from MSSQLServer to a word file.