Adding tables

E

Edward

Hi everybody
I know how to add a table and populate the cells with some data and move
between cells but I don't know how to get out of the table in VBA?
what im trying to do is adding a table then populate them with data that i
have imported from excel and kept in an array then get off the table and
right below it insert a new table and populate the cells with othre data

sub test()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow

add cells with some data here

get out the table cells
insert a new table
add data in the new table and repeat these steps for a about 10 tables.

my question is first how can i unselect and get out of a table in VBA
second how can I know where( which position on the page ) im inserting a
table , I need to know this because if there is little room for a new table I
don't want to insert it in the same page and i want to go to the next page
and insert the new table so position control is needed in my program
Thanks,
 
G

Greg Maxey

Edward,

I suppose that you would need a marker in the document to use to set a range
and evaluate the postion on the page. Let's create a bookmark in the
document called "TableIP".

Then run this code:

Sub ScratchMacro()
Dim y As Single
Dim oTbl As Word.Table
Dim oRng As Word.Range
Dim i As Long
'Loop 10 times
For i = 1 To 10
'Set range to TableIP range
Set oRng = ActiveDocument.Bookmarks("TableIP").Range
'Move the range up one paragraph so you don't interfere with the bookmark
oRng.Move wdParagraph, -1
'Select the range
oRng.Select
'find the vertical poistion of the selection in points
y = Selection.Information(wdVerticalPositionRelativeToPage)
'evaluate that postion and proceed accordingly
If y / 72 < 8 Then 'The less than value would depend on how many rows you
expect your table will have
'Add a paragraph to prevent tables from being connected together.
oRng.InsertBefore vbCr
add the table.
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 2, 5)
With oTbl
'Do your deed
End With
Else
oRng.InsertBefore vbCr
Selection.InsertBreak Type:=wdPageBreak
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 2, 5)
With oTbl
'Do your deed
End With
End If
Next i
End Sub
 
J

Jay Freedman

Hi everybody
I know how to add a table and populate the cells with some data and move
between cells but I don't know how to get out of the table in VBA?
what im trying to do is adding a table then populate them with data that i
have imported from excel and kept in an array then get off the table and
right below it insert a new table and populate the cells with othre data

sub test()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow

add cells with some data here

get out the table cells
insert a new table
add data in the new table and repeat these steps for a about 10 tables.

my question is first how can i unselect and get out of a table in VBA
second how can I know where( which position on the page ) im inserting a
table , I need to know this because if there is little room for a new table I
don't want to insert it in the same page and i want to go to the next page
and insert the new table so position control is needed in my program
Thanks,

Hi Edward,

For starters, don't use the Selection to "move around" in the table; just use
the Table object and its members to enter the data. Then you won't have to "get
out of" the table, because the cursor won't ever be in it. Something like this:

Sub test2()
Dim myRg As Range
Dim myTbl As Table
Dim Tnum As Integer

For Tnum = 1 To 10 ' or some other loop type

Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseEnd

Set myTbl = ActiveDocument.Tables.Add( _
Range:=myRg, NumRows:=2, NumColumns:=5, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)

With myTbl
.Cell(1, 1).Range.Text = "Name"
.Cell(1, 2).Range.Text = "Address"
' etc. for rest of header row

' now use a While or For loop to
' extract data from Excel and put it
' into cells in row 2
.Cell(2, 1).Range.Text = "name from Excel"
.Cell(2, 2).Range.Text = "address from Excel"
' etc., then
.Rows.Add
' Wend or Next

' after last row of data is added,
' delete the empty row that isn't needed
.Rows.Last.Delete
End With

' add a paragraph to separate the tables
Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseEnd
myRg.Text = vbCr

Next Tnum
End Sub

To keep the table together without breaking across pages, _do not_ rely on hard
page breaks -- they're a nightmare for document maintenance. Read
http://www.word.mvps.org/FAQs/TblsFldsFms/ControlPgBrksInTables.htm for
explanation. Mark the paragraphs in each row (except the last row) as "Keep with
next"; as each row is filled in, execute

myTbl.Rows.Last.Range.Paragraphs.KeepWithNext = True

and when the table is complete, set each row to not allow the row to break
across pages:

myTbl.Rows.AllowBreakAcrossPages = False
 
C

chris mcmullan

Hi this is my first post:

' now use a While or For loop to
' extract data from Excel and put it
' into cells in row 2
..Cell(2, 1).Range.Text = "name from Excel"
..Cell(2, 2).Range.Text = "address from Excel"
' etc., then
..Rows.Add
' Wend or Next

this part of your code i'm wondering how i can take inforatmion from and xml file and pass it to the cell in the table:

..Cell(2, 1).Range.Text = "Some sort code to set cell to and xml value")

hope you can help

chris
 
C

chris mcmullan

Hi this is my first post. this is a quetions about this part of your code

' now use a While or For loop to
' extract data from Excel and put it
' into cells in row 2
..Cell(2, 1).Range.Text = "name from Excel"
..Cell(2, 2).Range.Text = "address from Excel"
' etc., then
..Rows.Add
' Wend or Next

Wondering do you know the code to use to add an xml value to the cell in the table:

..Cell(2, 2).Range.Text = "so here some sort of code to set the cell to an xml value"

i'm using the visual basic editor that comes with word 2007
 
C

chris mcmullan

help
Hi everybody
I know how to add a table and populate the cells with some data and move
between cells but I don't know how to get out of the table in VBA?
what im trying to do is adding a table then populate them with data that i
have imported from excel and kept in an array then get off the table and
right below it insert a new table and populate the cells with othre data

sub test()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow

add cells with some data here

get out the table cells
insert a new table
add data in the new table and repeat these steps for a about 10 tables.

my question is first how can i unselect and get out of a table in VBA
second how can I know where( which position on the page ) im inserting a
table , I need to know this because if there is little room for a new table I
don't want to insert it in the same page and i want to go to the next page
and insert the new table so position control is needed in my program
Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top