-
Creating multiple tables
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:
[VBA]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 [/VBA]
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:
-
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.[VBA] 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)
[/VBA] Once you have all the tables in the document, you can refer to them by index[VBA] With ThisDocument.Tables(2)
'do stuff to tables
End With
[/VBA]
-
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:
[VBA]strText = strText & Textbox1 & vbTab & Textbox2 & vbCr
' (Pretend like the textboxes are you're columns)[/VBA]
No you just write the string to a range in the document (Can be selection.range or use Bookmark navigation..you pick)
Like:[VBA]
Dim oRange as Range
Set oRange = Selection.Range
oRange.InsertAfter strText [/VBA]
You now have a tabseparated block of text in you're document you can convert to a table like: [VBA]
oRange.ConvertToTable vbTab[/VBA]
Because you are working with a range object you can advance with adressing this table with:[VBA]
oRange.Tables(1)
[/VBA]
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: