Thread: [Solved] creating a report table
View Single Post
 
Old 05-02-2012, 04:11 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi kannan,

OK, so, if you know where you want the data to go, it's a simple matter of putting it there. Ideally, you'd collate all the data for a given location before inserting it there, though could could work through the data iteratively and output each portion to the appropriate place in the document as you go. Do note that if the same data needs to be replicated in multiple places, it only needs to be output to the document once - you then use cross-referencing for the replication.

Now, suppose you have a table somewhere that you want to add rows to and you know it's the third table. If you're working through the data iteratively, you can use code like:
Code:
Sub Demo()
Dim i As Long, StrData As String
StrData = "Column1,Column2,Column3,Column4,Column5,Column6,..."
With ActiveDocument.Tables(1).Rows
  .Add
  For i = 1 To .Last.Cells.Count
    .Last.Cells(i).Range.Text = Split(StrData, ",")(i - 1)
  Next
End With
End Sub
In this case 'StrData' is the name of whatever variable you're holding the data in. For demonstration purposes, a comma-delimiter is assumed.

Likewise, if you have a bookmarked destination (in this case, named 'MyBookmark') that you want to populate, with a single data stream that you want to turn into a table, you might use code like:
Code:
Sub Demo()
Dim StrHdr As String, StrData As String
StrHdr = "Column1,Column2,Column3,Column4,Column5,Column6"
StrData = "$A$1,$B$1,$C$1,$D$1,$E$1,$F$1" & vbCr & _
          "$A$2,$B$2,$C$2,$D$2,$E$2,$F$2" & vbCr & _
          "$A$3,$B$3,$C$3,$D$3,$E$3,$F$3" & vbCr & _
          "$A$4,$B$4,$C$4,$D$4,$E$4,$F$4" & vbCr & _
          "$A$5,$B$5,$C$5,$D$5,$E$5,$F$5" & vbCr & _
          "$A$6,$B$6,$C$6,$D$6,$E$6,$F$6" & vbCr & _
          "$A$7,$B$7,$C$7,$D$7,$E$7,$F$7" & vbCr & _
          "$A$8,$B$8,$C$8,$D$8,$E$8,$F$8" & vbCr & _
          "$A$9,$B$9,$C$9,$D$9,$E$9,$F$9" & vbCr & _
          "$A$10,$B$10,$C$10,$D$10,$E$10,$F$10"
StrData = StrHdr & vbCr & StrData
Call UpdateBookmark("MyBookmark", StrData)
With ActiveDocument.Bookmarks("MyBookmark").Range
  .ConvertToTable Separator:=","
  .Tables(1).Style = wdStyleTableMediumList2
End With
End Sub
 
Sub UpdateBookmark(BmkNm As String, NewTxt As String)
Dim BmkRng As Range
With ActiveDocument
  If .Bookmarks.Exists(BmkNm) Then
    Set BmkRng = .Bookmarks(BmkNm).Range
    BmkRng.Text = NewTxt
    .Bookmarks.Add BmkNm, BmkRng
  End If
End With
Set BmkRng = Nothing
End Sub
As I said, none of this is especially complicated.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote