View Full Version : "flexible" table help
Mr Doubtfire
10-10-2005, 09:07 AM
I have the following data.
col1     col2
AA        10
BB        20
CC        30
 
and the total for the above is 60 (for col2).
 
I am using a table to declare like this
Set Tbl1 = objApp.ActiveDocument.Tables.Add(Range:=Selection.Range, _
        NumRows:=50, NumColumns:=2)
therefore the minimum is 50 lines.  The problem I have is the maximum of lines could be 500 or even 1.  And I would put the total of lines after the table.
Could any one tell me how I could have a flexible number of lines table.
(Could it be possible to have ONLY one row by creating a table for every row!?)
Thanks.:help
fumei
10-10-2005, 07:23 PM
Uh, please try and post what you want.  However, since you asked....
How can you have a flexible number of lines (rows)?
Make the number of rows a variable...., and then use it.
Dim i As Integer
' make i = whatever you like
Set Tbl1 = objApp.ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=i, NumColumns:=2)
I am pretty sure this is not exactly what you want...but that is what you asked for.
Is it possible to have only ONE row?  Sure - make NumRows:=1.
Could you create a table for every row?  Sure, you can make a bunch of tables, if you are so inclined.
Oh, and
Set Tbl1 = objApp.ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=50, NumColumns:=2)
does not make a minimum of 50.  It makes a table with - quite explicitly - 50 rows.  Not 49, or 51, but 50.
The real question is:  do you know the number of rows you need BEFORE you create the table?  If the answer is yes, well then use that known numer as the number of rows.  If the answer is no, you do NOT know the number of rows needed before you create the table, then.....
Ask a better question.
Mr Doubtfire
10-11-2005, 04:01 PM
Sorry for not being clear.
"flexible" means I do not have a "certain" number of rows.  And I do not know how many I would have before creating the table.
My idea is to create one-row table(s) as long as the loop goes (from sql query) so as not to put ie. 50 rows even I am not using only less than 50 rows.
Also I understand I need to put "something" like 
    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph
in between tables, but that "something" would mean a gap between rows of the same table.
Therefore, I need to get a way to "glue" all the "one-row" table(s) into a big "flexible" table seaminglessly.
Thanks.
fumei
10-11-2005, 04:23 PM
Ah, this reminds me of another current thread.....
If you have a loop, surely there must be a way to get the number of iterations.  However, even so, how about making ONE table and adding rows - rather than individual tables for each record.  You can not "glue" one row tables together.
You could build a new table with the number of rows equal to the total existing table count.  That is, if there are 30 one row tables, make a new table with 30 rows, then move the contents of each one row table, one at a time, into the appropriate row of the new 30 row table.
It can be done, but it is sloppy design.  Here it is.
Sub SmooshNewTable()
Dim i As Integer
Dim var
i = ActiveDocument.Tables.Count
    ActiveDocument.Tables.Add Range:=Selection.Range, _
        NumRows:=i, NumColumns:=1, _
        DefaultTableBehavior:=wdWord9TableBehavior, _
        AutoFitBehavior:=wdAutoFitFixed
For var = 2 To i + 1
    ActiveDocument.Tables(1).Rows(var - 1).Range.Text = _
        Left(ActiveDocument.Tables(var).Rows(1).Range.Text, _
            Len(ActiveDocument.Tables(var).Rows(1).Range.Text) - 4)
Next
End Sub
Notes:
This creates the new table at the Selection point.  As written the selection point is BEFORE the existing tables.  That is why For var = 2 to i + 1; when the new table is created IT becomes Tables(1), the existing ones now start at 2.
If you are going to have the new table in a different location, this MUST be adjusted.
The reason for the range.text using Len(blah blah, -4) is that you need to trim off the end-of-cell marker (which is two characters), and the end-of-row marker (also two characters).
Mr Doubtfire
10-11-2005, 06:26 PM
Thank you for the suggestion.
I have another one that seems to be working as followed.  Please advice.: pray2: 
Public objApp As Word.Application
Public Tbl1 As Table
Private Sub Command1_Click()
     
    On Error Resume Next
    Set objApp = GetObject(, "Word.Application")
    If objApp Is Nothing Then
        Set objApp = CreateObject("Word.Application")
    End If
    With objApp
        .Documents.Add , , , True
        .Visible = False
    End With
     
    For intCounter1 = 1 To 3
    If intCounter1 = 1 Then
        Set Tbl1 = objApp.ActiveDocument.Tables.Add _
                  (Range:=Selection.Range, NumRows:=1, NumColumns:=2)
        Tbl1.Columns(1).Width = 100
        Tbl1.Columns(2).Width = 100
    Else
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.InsertRows 1
    End If
    
    Tbl1.Cell(intCounter1, 1).Range.Text = "Table1 - col1" & intCounter1
    Tbl1.Cell(intCounter1, 2).Range.Text = "Table1 - col2"
    
   
   Next
 
    objApp.ActiveDocument.SaveAs FileName:="c:\test1.doc"
     
    objApp.Quit False
    Set objApp = Nothing
     
    MsgBox "done"
     Set Tbl1 = Nothing
   
        objApp.Selection.MoveEnd Unit:=wdTable, Count:=1 _
                      '<- move to end of table
       objApp.Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove _
                     '<- move foward 1 line
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.