PDA

View Full Version : Creating multiple tables



dzulawski
04-29-2005, 07:20 AM
Hello,

I'm trying to mash together some code to speed along a process of making certain types of documents. Basically I have a form where I fill out all the information I need, press a generate button, and it's transfered to the word document in a certain format. Type in another set of information, press generate, and another table is placed underneath the first one. And so on...

I have the code:

Set tableNew = ThisDocument.Tables.Add(Selection.Range, X, Y)
With tableNew
.Rows(1).Shading.BackgroundPatternColor = wdColorGray125
.Cell(1, 1).Range.InsertAfter "Conveyor Section"
.Cell(1, 2).Range.InsertAfter "Device Type"
.Cell(1, 3).Range.InsertAfter "Device Sub-Type"
.Cell(1, 4).Range.InsertAfter "Functional Description"
.Cell(1, 5).Range.InsertAfter "P/F"
.Cell(1, 6).Range.InsertAfter "Comments"
.Cell(1, 7).Range.InsertAfter "Date"
End With

My question is: How do I create another table below the first one? Do I need to keep track of some kind of index number when I reference the 2nd, 3rd, etc table, or can I reference the cells like the code: put "this" in cell(1,1) for table 2?

Many thanks for any help. I'm a PLC programmer, so feel free to roll your eyes at my (hopefully) silly questions :whistle:

Killian
04-29-2005, 08:10 AM
Hi and welcome to VBAX :hi:

You're inserting the table at the selection, so you'll need to move the selection to where you want the next table to be. This will probably mean moving it to the end of the document, using Selection.Typetext to add a carriage return (or the table will be appended to the last one) and then adding the new table. Set tableNew = ThisDocument.Tables.Add(Selection.Range, X, Y)
With tableNew
.Rows(1).Shading.BackgroundPatternColor = wdColorGray125
.Cell(1, 1).Range.InsertAfter "Conveyor Section"
.Cell(1, 2).Range.InsertAfter "Device Type"
.Cell(1, 3).Range.InsertAfter "Device Sub-Type"
.Cell(1, 4).Range.InsertAfter "Functional Description"
.Cell(1, 5).Range.InsertAfter "P/F"
.Cell(1, 6).Range.InsertAfter "Comments"
.Cell(1, 7).Range.InsertAfter "Date"
End With
Selection.EndKey Unit:=wdStory
Selection.TypeText Chr(13)
Once you have all the tables in the document, you can refer to them by index With ThisDocument.Tables(2)
'do stuff to tables
End With

MOS MASTER
04-29-2005, 11:30 AM
Hi,
Welcome to VBAX :hi:

Seams to me you're code is only showing the header row of the table? And if so it seams to me this code is going to be pretty long! :rofl:

I think a lot off data needs to be put in rows under the headersection right?

If so the most easy thing to do is to create a string with a separator in it like:
strText = strText & Textbox1 & vbTab & Textbox2 & vbCr
' (Pretend like the textboxes are you're columns)

No you just write the string to a range in the document (Can be selection.range or use Bookmark navigation..you pick)
Like:
Dim oRange as Range

Set oRange = Selection.Range
oRange.InsertAfter strText

You now have a tabseparated block of text in you're document you can convert to a table like:
oRange.ConvertToTable vbTab

Because you are working with a range object you can advance with adressing this table with:
oRange.Tables(1)

And do some formatting stuff.

Loading everything to a string and converting it saves a lot of time and is more reliable dan looping through the cells.

If you need a little example let me know and I'll make it for yah! (If so attach you're total code in a zipped file so I can see what you're really doing)

Enjoy! :whistle: