Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to format tables in MS Word

Status
Not open for further replies.

timothyyoung

Technical User
Apr 26, 2007
7
EU
I am trying to create a macro in MS word using VBA that builds a 4 column table. It will need to use the preceding numbering format before the table and when the table is inserted (using the macro) continues the numbering format inside the table (when a user adds text and tabs between cells). The completed table would look like the following for example.

..

3
4
5
6

Table goes here

cell 1 cell 2 cell 3 cell 4

6.1 text here 6.1.1 text here

text here 6.1.2 text here

6.2 text here 6.2.1 text here

text here 6.2.2 text here

etc

I have created the macro on the menu bar but havn't got a clue when it comes to VBA. Any help would be much appreciated.
 
cell 1 cell 2 cell 3 cell 4

6.1 text here 6.1.1 text here (Row 1)

text here[/color red] 6.1.2 text here (Row 2)

6.2 text here 6.2.1 text here (Row 3)

text here [/color red] 6.2.2 text here (Row 4)

If this is the structure - specifically that the second line is a different row - and the red text is supposed to be 6.1 and 6.2 (continued) respectively, you have a problem. Because that red text is a different cell, it is a different paragraph, and consecutive numbering will not work.

Please clarify: "when a user adds text and tabs between cells"

Are you trying to make some sort of automatic numbering as the user adds text? I can't see how you can do that. If you are asking about how you could PUT the numbering in - in advance of the user putting text, that may be possible.

Please describe again, in more detail. As it stands this may not be possible.

Gerry
My paintings and sculpture
 
Thanks for the reply Gerry. A bit of an amendment to my original post is that the 2nd numbering column (6.1.1 and 6.1.2) will be in the same row as the first numbering cell (6.1) as follows.

column 1 column 2 column 3 column 4

Title here Title here Title here Title (row 1

6.1 text here 6.1.1 text here (Row 2)

6.1.2 text here (Row 2)

6.2 text here 6.2.1 text here (Row 3)

6.2.2 text here (Row 3)

etc.

Are you trying to make some sort of automatic numbering as the user adds text? Yes exactly. Although some numbering would be added as the table is created (before the user adds text)

Row 1 (the titles) and the first numbering cell (6.1) and possibly the 6.1.1 cell would be put in when the table is created (before the user types text). The rest would be put in as the user edits the table. Do u think its possible now?
 
Huh? You mean:

Col1 Col2 Col3 Col4
6.1 text 6.1.1 text 6.1.2 text something else
6.2 text 6.2.1 text 6.2.2 text something else


Code:
Sub InsertTable()
Dim oTable As Word.Table
Dim oCol As Column
Dim var
    ActiveDocument.Tables.Add Range:=Selection.Range, _
        NumRows:=2, NumColumns:= 4, _
        DefaultTableBehavior:=wdWord9TableBehavior, _
        AutoFitBehavior:= wdAutoFitFixed

Set oTable = Selection.Tables(1)
For var = 1 To 3
    Set oCol = oTable.Columns(var)
    oCol.Select
    Selection.Style = "heading " & var
Next
End Sub
Now this will do your four column table, putting headings for the first three. That is:

1.0 1.1 1.2 Col4 no numbering
2.0 2.1 2.2 Col4 no numbering

The problem is that you want to use the SAME number level at the paragraph outside the table. This is a problem.


Gerry
My paintings and sculpture
 
Not quite. I need the format to be like this:

Col1 Col2 Col3 Col4
6.1 text 6.1.1 text
text 6.1.2 text
6.2 text 6.2.1 text
text 6.2.2 text

Sorry if I havn't made it more clear.

Thanks.
 
To make things more clear:

Col1 Col 2 Col 3 Col 4

Description Task Notes

6.1 Make cup of tea 6.1.1 Boil Kettle blabla
6.1.2 Put sugar

6.2 blablabla 6.2.1 blabla bla
6.2.2 bla

The first row is the titles
The 2nd row is for 6.1. This includes 6.1.1 AND 6.1.2 (not seperate rows).

The 3rd row is for 6.2 and 6.2.1 and 6.2.2.

Thanks.
 
Problem remains. You are skipping numbering.

Col 1 6.1 (numbering)
Col 2 text - no numbering
Col 3 6.1.1 numbered + text
Col 4 6.1.2 numbered + text

Col 1 6.2 (numbering)
Col 2 text - no numbering
Col 3 6.2.1 numbered + text
Col 4 6.2.2 numbered + text

Sorry, perhaps someone else can help, but I am stuck with producing the effect you want in any automated way.

Gerry
My paintings and sculpture
 
OK. Thanks for trying anyway Gerry.
Is there someone who is an expert at VBA I can email?

 
Good luck with finding an expert. I doubt it though, as I believe it is not possible, at least as you describe it. At least as an automatic numbering, as the user enters new text.

It can be done after the fact though.

If you DO find a way, be sure to post the solution!


Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top