use of loops to cycle through table cells

dusty

Registered User.
Local time
Today, 07:42
Joined
Aug 20, 2009
Messages
42
So I am trying to use code to create a table in word.

So far I have managed to insert a table of variable rows and columns depending on values of x,y. This table is placed into a specified bookmark and then the bookmark is replaced after the table so that another table if required can be enetered.

Once the table is positioned I want to be able to insert formfields into each cell. The selection is in cell one so by cycling the selection to the right one step it will move to the next cell.

I want to be able to write a loop that cycles one step at a time and inserts a formfield in each cell.

x*y will give the number of cells hence number of cycles.

so i = 1 is the first case no movement of selection needed so i can use the code

Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput

to insert a formfield into cell 1

So i need to write the case for i+1 to n

where n= x*y

could someone please point me in teh right direction :)

thanks
 
Ok so I have achieved almost want i require.

Please see attached file.

However I am having difficulty formatting some of the cells.

At the moment it is set so that the first row is merged across the six columns works perfect formfields are inserted into the correct cells.

however if I try and merge a row lower than 1 say row 5 I get an error its something to do with the way the form fields are cycled through and inserted but i cant work it out.

i want to be able to have row one fully merged then rows beneath in six columns and then say also row 5 fully merged.

here is the code i have written. If i change teh value in teh if statement from 1 to say 5 i get an error.

Sub table()
a = 1
y = 12
x = 6
Dim table As Range
Set table = ActiveDocument.Bookmarks("table").Range
table.Select
For i = 1 To y
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
x, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
End With

If i = 1 Then
Selection.Expand
Selection.SelectRow
Selection.Cells.Merge
End If

For j = 1 To ActiveDocument.Tables(1).Columns.Count

ActiveDocument.FormFields.Add _
Range:=ActiveDocument.Tables(1).Cell(rownum, j).Range, _
Type:=wdFieldFormTextInput
Set myFF = ActiveDocument.Tables(1).Cell(rownum, j).Range.FormFields(1)
myFF.Name = "Text_" & rownum & a
Set myFF = Nothing

a = a + 1

Next j

Selection.MoveDown unit:=wdLine, Count:=1
Next i
Selection.MoveDown unit:=wdLine, Count:=y + 1
Selection.TypeParagraph
ActiveDocument.Bookmarks.Add Name:="table"
End Sub
 

Attachments

Users who are viewing this thread

Back
Top Bottom