Word tables and DataSets - FINALLY!!!!!!!!!

  • Thread starter Thread starter scorpion53061
  • Start date Start date
S

scorpion53061

I figured it out.........

I felt I owed it to the groups I harassed with this quesiton :) to tell the
solution....

special thanks to Ken and William Ryan who kept at it with me!!

Originally used a string. But all of you in the information "business" know
sometimes we have to work with large amounts of data and strings do have
limitaitons believe it or not on the amount of data it will carry......so I
used an Array. It is actually slower using an array. You are redoing the
text each loop. But of course as usual I am open to suggestions....If anyone
sees anything here that will not work with Word 2000 and up I would
appreciate a heads up.......


It is messy and it aint pretty but it works :)

Can anyone tell I am happy?

'establishing counts
Dim rows As Integer = dscopy1.Tables.Item(0).Rows.Count
Dim columns As Integer = dscopy1.Tables.Item(0).Columns.Count
Dim r As Integer
Dim c As Integer
Dim Word As New Word.Application
Word.Visible = True
'opening word
Dim oDoc As New Word.Document
oDoc = Word.Documents.Add()
'make an array of the data
Dim DataArray(rows, columns) As Object
'filling the array
For c = 0 To columns - 1
DataArray(r, c) = dscopy1.Tables(0).Columns.Item(c).ColumnName
For r = 0 To rows - 1
DataArray(r, c) = dscopy1.Tables(0).Rows(r).Item(c)
Next
Next
'where we are going with the data
Dim oRange = oDoc.Content.Application.Selection.Range
With oDoc.Application.Selection.Font
.Bold = False
End With
'filling orange for table using array due to string limitation
For r = 0 To rows - 1
For c = 0 To columns - 1
oRange.Text = oRange.Text & DataArray(r, c) & vbTab
Next
Next
'converting our text to table
oRange.ConvertToTable(Separator:=wdSeparateByTabs,
NumColumns:=dscopy1.Tables(0).Columns.Count, _
NumRows:=dscopy1.Tables(0).Rows.Count, AutoFitBehavior:=wdAutoFitContent)
'correction needed here to get it to cooperate
With oDoc.Application.Selection.Range.Tables.Item(1)
.AllowAutoFit = True
End With
'selecting top row
oDoc.Application.Selection.Tables.Item(1).Rows.Item(1).Select()
'SelectRow
'for header row
oDoc.Application.Selection.InsertRowsAbove(1)
With oDoc.Application.Selection.Font
.Bold = True
End With
'doing this late........havent had time to test whether it will matter
oDoc.Application.Selection.Rows.Item(1).HeadingFormat = True
'header row
For c = 0 To columns - 1

oDoc.Application.Selection.Tables.Item(1).Rows.Item(1).Cells.Item(c +
1).Range.Text = dscopy1.Tables(0).Columns.Item(c).ColumnName
Next
oDoc.ActiveWindow.Activate()
 
Hi Scorpion,

Yes you can speed it up.
Dim oRange = oDoc.Content.Application.Selection.Range
With oDoc.Application.Selection.Font
.Bold = False
End With
'filling orange for table using array due to string limitation

dim sb as new text.stringbuilder
For r = 0 To rows - 1
For c = 0 To columns - 1
sb.append( DataArray(r, c)) sb.append(vbTab)
Next
Next
oRange.Text = sb.tostring

But here it gives (as in your code) an extra tab, when you look at the
sample I did send (it looks almost the same, except the Word connections),
I did avoid that.


Cor
 
Hi Scorpion,

I did not look well to it,

I think you do not need that array,

I usex your word code (I told you that is something to far behind me to
help you in that part I did change only dscopy for ds). This should do it
also (did not test is). And minimal twice as fast.
(I also included the code to avoid the last tab at the end)
\\\
Dim c As Integer
Dim r As Integer
Dim sb As New System.Text.StringBuilder
For r = 0 To ds.Tables(0).Rows.Count - 1
For c = 0 To ds.Tables(0).Columns.Count - 1
sb.Append(ds.Tables(0).Rows(r)(c).ToString)
If r <> ds.Tables(0).Rows.Count - 1 AndAlso _
c <> ds.Tables(0).Columns.Count - 1 Then
sb.Append(vbTab)
End If
Next
Next
///
Dim Word As New Word.Application
Word.Visible = True
'opening word
Dim oDoc As New Word.Document
oDoc = Word.Documents.Add()
Dim oRange = oDoc.Content.Application.Selection.Range

oRange.Text = sb.ToString
With oDoc.Application.Selection.Font
.Bold = False
End With
'filling orange for table using array due to string limitation
oRange.ConvertToTable(Separator:=wdSeparateByTabs,
NumColumns:=ds.Tables(0).Columns.Count, _
NumRows:=ds.Tables(0).Rows.Count, AutoFitBehavior:=wdAutoFitContent)
'correction needed here to get it to cooperate
With oDoc.Application.Selection.Range.Tables.Item(1)
.AllowAutoFit = True
End With
'selecting top row
oDoc.Application.Selection.Tables.Item(1).Rows.Item(1).Select()
'SelectRow
'for header row
oDoc.Application.Selection.InsertRowsAbove(1)
With oDoc.Application.Selection.Font
.Bold = True
End With
'doing this late........havent had time to test whether it will matter
oDoc.Application.Selection.Rows.Item(1).HeadingFormat = True
'header row
For c = 0 To columns - 1
oDoc.Application.Selection.Tables.Item(1).Rows.Item(1).Cells.Item(c +
1).Range.Text = ds.Tables(0).Columns.Item(c).ColumnName
Next
oDoc.ActiveWindow.Activate()
 
You are saying then that "sb" should be what I am converting to table for
the range?
 
Cor,

send me an email. ([email protected])

remove nospamhere

I want to send you the word table this stringbuilder code created but I
can't post the document here without someone getting angry.
 
Back
Top