I have been working on a program over the last several days that that opens up an existing Word Document, goes to a certain place in the document, and creates a numbered list from a SQL Query. Everything is working great except the list is created in the document in the reverse order that it is retrieved from the database. For example: the SQL query retrieves the records in the following order:
Record 1
Record 2
Record 3
The list in the word document displays as:
1) Record 3
2) Record 2
3) Record 1
I have been unable to determine what I am doing wrong. Here is my code. Thank you for your help.
Public Sub addTableVendorClar(ByRef oWord As Word.Application, ByRef oWordDoc As Word.Document)
Dim bolFound As Boolean = False
Dim rngRange As Word.Range = oWordDoc.Range(0, 0)
With rngRange.Find
.ClearFormatting()
.Replacement.ClearFormatting()
.Text = "[tableVendorClar]"
.Wrap = Word.WdFindWrap.wdFindContinue
.MatchCase = False
End With
bolFound = rngRange.Find.Execute()
If bolFound Then
rngRange.Delete(Unit:=Word.WdUnits.wdCharacter, Count:=1)
' create outline
With oWord.ListGalleries(2).ListTemplates(1).ListLevels(1)
.NumberFormat = "%1)"
.ResetOnHigher = 0
.StartAt = 1
.LinkedStyle = ""
End With
oWord.ListGalleries(2).ListTemplates(1).Name = ""
rngRange.ListFormat.ApplyListTemplateWithLevel(ListTemplate:= _
oWord.ListGalleries(2).ListTemplates(1))
strSql = strSQLVenClar
If connStr.State <> ConnectionState.Open Then
connStr.Open()
End If
Dim cmd1 As New System.Data.SqlClient.SqlCommand(strSql, connStr)
cmd1.CommandTimeout = CommandTimeoutV
dr = cmd1.ExecuteReader
Dim i As Integer = 1
Dim strText As String
While dr.Read()
If i <> 1 Then
rngRange.FormattedText.Text = Chr(10)
End If
strText = dr.Item("ClarificationText").ToString
rngRange.FormattedText.Text = strText
i += 1
End While
cmd1.Connection.Close()
connStr.Close()
dr.Close()
End If
End Sub
Record 1
Record 2
Record 3
The list in the word document displays as:
1) Record 3
2) Record 2
3) Record 1
I have been unable to determine what I am doing wrong. Here is my code. Thank you for your help.
Public Sub addTableVendorClar(ByRef oWord As Word.Application, ByRef oWordDoc As Word.Document)
Dim bolFound As Boolean = False
Dim rngRange As Word.Range = oWordDoc.Range(0, 0)
With rngRange.Find
.ClearFormatting()
.Replacement.ClearFormatting()
.Text = "[tableVendorClar]"
.Wrap = Word.WdFindWrap.wdFindContinue
.MatchCase = False
End With
bolFound = rngRange.Find.Execute()
If bolFound Then
rngRange.Delete(Unit:=Word.WdUnits.wdCharacter, Count:=1)
' create outline
With oWord.ListGalleries(2).ListTemplates(1).ListLevels(1)
.NumberFormat = "%1)"
.ResetOnHigher = 0
.StartAt = 1
.LinkedStyle = ""
End With
oWord.ListGalleries(2).ListTemplates(1).Name = ""
rngRange.ListFormat.ApplyListTemplateWithLevel(ListTemplate:= _
oWord.ListGalleries(2).ListTemplates(1))
strSql = strSQLVenClar
If connStr.State <> ConnectionState.Open Then
connStr.Open()
End If
Dim cmd1 As New System.Data.SqlClient.SqlCommand(strSql, connStr)
cmd1.CommandTimeout = CommandTimeoutV
dr = cmd1.ExecuteReader
Dim i As Integer = 1
Dim strText As String
While dr.Read()
If i <> 1 Then
rngRange.FormattedText.Text = Chr(10)
End If
strText = dr.Item("ClarificationText").ToString
rngRange.FormattedText.Text = strText
i += 1
End While
cmd1.Connection.Close()
connStr.Close()
dr.Close()
End If
End Sub