PDA

View Full Version : Solved: Creating new table in Word from data in many tables



Solrac3030
10-19-2009, 11:36 AM
I'm trying to create a new table in Word from data that is in many tables on the same document. I need to copy the name and the number from the first cell in each row of every table in the document and then put that information into a new table at the end of a document. I need to write this so that the user just has to click on a menu item and it will create the new table with all the information at the end of the document. Need ideas as to how to start on this.

Name 1 (#123456/Capitola CA August 28, 2009
Name 2 (#111111/Dallas TX) September 23, 2009

Name 3 (#222222/Colonial Beach VA) September 21, 2009

Name 4 (#333333/Ojai CA) September 22, 2009

Name 5 (#444444/Los Angeles CA) August 17, 2009

Name 6 (#555555/Sacramento CA) September 25, 2009

Name 7 (#987654/Los Angeles CA) September 18, 2009
Name 8 (#66666/Grand Rapids MI) September 23, 2009

Name 9 (#77777/Los Angeles CA) September 28, 2009

Name 10 (#88888/Austin TX) September 18, 2009

Name 11 (#999999/Mill Valley CA) September 24, 2009

Name 12 (#000000/Los Angeles CA) September 28, 2009


The above is one of the tables, it has two columns, the first contains the information I need which is just the name and then I need the number between the pound sig and the forward slash. The new table will be a two column table where the name will be on the first column and the number on the second column.

Any ideas as to how to go about this would be greatly appreciated. I am familiar with VBA and how to do certain things but right now I do not have an idea on how to start on this.

The code for this would sit on a global template and then I would run it from there. I can do the menu items once I have the code written.

Edited 20-Oct-09 by geekgirlau. Reason: remove personal information

Tinbendr
10-19-2009, 07:10 PM
Here's the summary part. See if this works for you.

Option Explicit
Sub SummaryTable()

Dim Rng As Range
Dim Tbl As Table
Dim TblRows As Row
Dim TblArray() As String
Dim Counter As Integer

Counter = 1
For Each Tbl In ActiveDocument.Tables
For Each TblRows In Tbl.Rows

ReDim Preserve TblArray(Counter)
Set Rng = Tbl.Cell(Counter, 1).Range

If Len(Rng) > 2 Then
With Rng
.Collapse wdCollapseStart
.MoveEndUntil "#"
.MoveEnd wdCharacter, -2
TblArray(Counter) = .Text

.MoveEndUntil Chr(13)
.MoveStartUntil "#"
.MoveStart wdCharacter
.MoveEndUntil "/", wdBackward
.MoveEnd wdCharacter, -1
TblArray(Counter) = TblArray(Counter) & "," & .Text
Counter = Counter + 1
End With
End If
Next
Next

Set Rng = ActiveDocument.Range
Rng.Collapse wdCollapseEnd

For Counter = 1 To UBound(TblArray)
Rng.InsertAfter TblArray(Counter) & vbCr
Next

Rng.ConvertToTable wdSeparateByCommas


End Sub
This reads the data from the table and stores it in an array. It inserts the data from the array at the end of the document, then converts the text into a table.

fumei
10-20-2009, 10:28 AM
tinbendr, very clever use of array in your code. Here is an slightly alternate way. It uses .Words of the range, and will ONLY work if the text structure is as stated.
Sub SummaryTableGerry()

Dim Rng As Range
Dim Tbl As Table
Dim TblRows As Row
Dim TblArray() As String
Dim Counter As Integer
Counter = 1
For Each Tbl In ActiveDocument.Tables
For Each TblRows In Tbl.Rows
ReDim Preserve TblArray(Counter)
Set Rng = Tbl.Cell(Counter, 1).Range
If Len(Rng) > 2 Then
TblArray(Counter) = _
Rng.Words(1) & Rng.Words(2) & _
"," & Rng.Words(4)
Counter = Counter + 1
End If
Next
Next

Set Rng = ActiveDocument.Range
Rng.Collapse wdCollapseEnd
For Counter = 1 To UBound(TblArray)
Rng.InsertAfter TblArray(Counter) & vbCr
Next
Rng.ConvertToTable wdSeparateByCommas
End Sub
Just to further clarify:

EXAMPLE: Name 1 (#123456/Capitola CA August 28, 2009

Words(1) = "Name " (note the trailing space)
Words(2) = "1 " (note the training space)
Words (3) not used, this is "(#"
Words(4) = "123456" (note NO trailing space!, as Words(5) is the "/")

Thus Words(1), Words(2) = "Name 1", add the "," and then Words(4) "123456".

Thus the text to put into the array is a single operation

TblArray(Counter) = _
Rng.Words(1) & Rng.Words(2) & _
"," & Rng.Words(4)

and does not require moving back and forth, and searching for specific characters (the "#" and the "/").

But again, it will only work if the text structure is as stated.

Tinbendr
10-20-2009, 01:50 PM
TblArray(Counter) = _
Rng.Words(1) & Rng.Words(2) & _
"," & Rng.Words(4)

Very nice! I'll have to remember this.

fumei
10-20-2009, 02:36 PM
Using .Words can be very handy, but be sure to test carefully because Word has some odd considerations of what is a "word".

BTW: you can also use .Next (and "word" as a Unit) with range objects and this can be handy with a .Find operation.

Say you have the text:

This is a single page document.

Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(FindText:="page", _
Forward:=True) = True
MsgBox "The next word is: " & _
r.Next(Unit:=wdWord, Count:=1) & vbCrLf & _
"The previous word is: " & _
r.Next(Unit:=wdWord, Count:=-1)
displays:

The next word is: document
The previous word is: single

Note that for the previous word you could also use:

r.Previous(Unit:=wdWord, Count:=1)

Obviously to get the previous word using .Next, you need a count of minus 1...or use .Previous with a count of +1.

Same thing.

Warning though again. Word can have some peculiar considerations for "word"

Note, you can also use different Units.

This is the paragraph BEFORE the search word.<p>
This is the sentence before the search word. This is a single page document.
Do While .Execute(FindText:="page", _
Forward:=True) = True
MsgBox "The next word is: " & _
r.Next(Unit:=wdWord, Count:=1) & vbCrLf & _
"The previous sentence is: " & _
r.Next(Unit:=wdSentence, Count:=-1)

displays

The next word is: document
The previous sentence is: This is the sentence before the search word.

Do While .Execute(FindText:="page", _
Forward:=True) = True
MsgBox "The next word is: " & _
r.Next(Unit:=wdWord, Count:=1) & vbCrLf & _
"The previous paragraph is: " & _
r.Next(Unit:=wdParagraph, Count:=-1)
displays

The next word is: document
The previous paragraph is: This is the paragraph BEFORE the search word.

Solrac3030
10-20-2009, 03:17 PM
Thank you very much for your help with this. I will look through the code and apply to the document I have and see how that work. If I run into any problems I cannot figure out I will add a post to this. If no problems beyond me I will close the thread. Than you again for your fast response.