LISTING 6. A Method to Copy a SQL Server Data Source into a Word Document as Sentences
Sub openProcedureOnRemoteServer()
Dim cnn1 As ADODB.Connection
Dim rst1 As ADODB.Recordset
Dim int1 As Integer
Dim strSentence As String
'Open connection to NorthwindCS database
'on cab2200 server
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=cab2200;" & _
"Catalog=NorthwindCS;" & _
"User Id=SQLMagUser;Password=sqlmag;"
cnn1.Open strCnn
'Open employee table
Set rst1 = New ADODB.Recordset
rst1.CursorType = adOpenForwardOnly
rst1.LockType = adLockReadOnly
rst1.Open "[Ten Most Expensive Products]", _
cnn1, , , adCmdStoredProc
'Format and print prices for 10 products
Selection.Font.Size = 12
If Selection.Font.Bold = True Then Selection.Font.Bold = wdToggle
For int1 = 1 To 10
strSentence = rst1.Fields(0) & " has a unit price " & _
"of $" & rst1.Fields(1) & "." & vbCrLf
Selection.TypeText strSentence
rst1.MoveNext
Next int1
'Insert and format header sentence
ActiveDocument.Content.InsertBefore "These are the ten " & _
"most expensive products." & vbCrLf
With ActiveDocument.Range.Sentences(1)
.Bold = wdToggle
.Font.Size = 14
End With
'Close the connection and recover the resource
cnn1.Close
Set cnn1 = Nothing
End Sub