Build table in Word 2010

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Build table in Word 2010

Post by Sundog »

In Word 2010, I need to populate a table with data from elsewhere in the document. The document contains Heading 1, 2, & 3s and text. Some paragraphs are followed by SIGN-OFF SOMEBODY.

Example:
8.4.2 INSPECT for loose nuts.
<para mark>
SIGN-OFF WORKER
<para mark>
8.4.3 NOW go do something else.
8.4.4 THEN do something different.
8.4.5 EXAMINE floor for stains.
<para mark>
SIGN-OFF VENDOR
<para mark>
etc.

The initial Table would exist with an initial blank row, and would end up looking like this:
8.4.2<xref> | INSPECT for loose nuts. | SIGN-OFF WORKER
8.4.5<xref> | EXAMINE floor for stains. | SIGN-OFF VENDOR

So I need to step thru my document looking for SIGN-OFF, capture it and the paragraph of text above it, and insert those into my table along with a cross-reference to the paragraph above the SIGN-OFF. Right now I'm doing all this manually, using View > Split, Insert > Cross-reference, and a whole lot of copying and pasting.

The attachment shows more. The Performance Table at the shows an end result, after some text modifications.

Should I attempt to populate an array with this info and transfer it into a table, or do one SIGN-OFF at a time until I reach the EOF? Any help or suggestions would be most welcome.
You do not have the required permissions to view the files attached to this post.
Sundog

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Build table in Word 2010

Post by macropod »

If you use one of Word's Heading Styles for the various sign-off locations, in the table you can then insert a cross-reference to each of those headings. From then on, to update the table should require nothing more complicated than a Print Preview of Ctrl-A, F9. No vba required.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Build table in Word 2010

Post by Sundog »

Paul, thanks for the reply. I am using Word heading styles for the locations. I am doing exactly what you describe.

My issue is, how to AUTOMATE the process via a macro/VBA. I want to Insert > Cross-Reference a heading hyperlink in my table, linking to a particular piece of text earlier in the document, once I've found it (as I described in the first submission in this thread, above). If you have time, see my attachment for what the end result might look like (tables in the back of the doc).

Now imagine 157 Insert > Cross-References from Heading 3s in Section 8, not just 5 as in my example doc.
Sundog

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Build table in Word 2010

Post by HansV »

Your sample document shows a table for each Heading 1 paragraph that has at least one SIGN-OFF under it, so it doesn't seem to be as "simple" as filling a table. Would you create all those tables manually, or should the macro create the subheadings and tables under ATTACHMENT 1 – SIGN-OFF SHEET?
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Build table in Word 2010

Post by Sundog »

The Prerequisites and Performance tables would already exist in Attachment 1, with their heading row and column headings in place, and with one blank row below the heading row.

If it would help, each last cell in each heading row could have an existing bookmark (PreTbl and PerfTbl), so a tab from there would move to the first non-blank cell in the blank row.
Sundog

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Build table in Word 2010

Post by HansV »

I think this would be very complicated. Moreover, I don't see how the example tables in your document have been derived. Why, for example, does "Before starting work, Designee CONDUCT task preview/pre-job brief per AD 04-W3030." become "Conducted pre-job brief per AD 04-W3030."? (None of the other table entries corresponds exactly to the text higher up)
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Build table in Word 2010

Post by Sundog »

Sorry if I didn't make everything clear. What I meant by "The Performance Table at the [Attachment 1] shows an end result, after some text modifications." above, is that the paragraph text that was placed in the table was modified and shortened by human intervention.

The table entry "8.4.2<xref> | INSPECT for loose nuts. | SIGN-OFF WORKER" might have started off as "INSPECT for loose nuts, un-tight nuts, almost falling off the bolt nuts, nuts that give the appearance of possibly loosening some time in the future, and any other way a nut might be said to be loose."

As I indicated above, it would be a great help to have this entire paragraph text inserted in the middle cell. What I'm trying to avoid is having to go back to 8.4.2 in the document, read the text there, go back to the table, craft a shortened version of the 8.4.2 text, and move on, 157 times. Work could be done just at the table in the Attachment.
Sundog

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Build table in Word 2010

Post by HansV »

I don't see a way to do that at the moment, but I'll think about it tomorrow.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Build table in Word 2010

Post by Sundog »

I appreciate your willingness to help!

Here are my thoughts on populating the Performance table, with some semi-cryptic abbreviations. (The "template" is the skeleton document that my writers fill in.)

But I don't know how to execute some of these steps.

STEPS
In template, put bkmk S8 at start of sect.8
In template, put bkmk PT1 in left cell of blank row at top of Perf tbl in Att.1
size Perf tbl Initials col to accept DESIGNEE____ (4 underscores)
In template, put bkmk S9 at start of sect.9, to serve as an EOF marker


GrabSO macro
Goto bkmk S8
Find Down SIGN-OFF, match case, Find Next
right arrow
Shift-end
left arrow
copy
selection = variable C
right arrow
insert bkmk S1

GrabTx macro
go up until hdg 1,2,3,4,5,or 6 is found
select entire para
selection=variable B

GrabNo macro
determine para heading no.
selection=variable A

PopTbl macro
goto bkmk PT1
insert xref corresponding to A
tab
insert B
tab


SO macro
get variable C
goto its end
add ____ four underscores
call this variable D

insert D

Continue macro
goto bkmk SO1
Find Down SIGN-OFF, match case, Find Next
right arrow
Shift-end
left arrow
copy
selection = variable C
right arrow
delete bkmk S1
insert bkmk S1

End macro
continue until you reach bkmk S9.
Sundog

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Build table in Word 2010

Post by HansV »

I'm not able to fulfill all requirements: I don't know how to set cross references, so the paragraph numbers in the tables are fixed text, and they don't act as hyperlinks, but hopefully the following will help at least a bit. It assumes that you have created the tables, and set bookmarks PreTbl and PerfTbl as you indicated higher up in this thread.

The first procedure FillTables is the macro to run. The second procedure FillOneTable is called twice from FillTables, once for Prerequisites and once for Performance.

Code: Select all

Sub FillTables()
    Dim Pos1 As Long, Pos2 As Long, Pos3 As Long
    Application.ScreenUpdating = False
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Style = ActiveDocument.Styles(wdStyleHeading1)
        .Execute FindText:="PREREQUISITES"
        Pos1 = Selection.Start
        Selection.Collapse Direction:=wdCollapseEnd
        .Execute FindText:="PERFORMANCE"
        Pos2 = Selection.Start
        Selection.Collapse Direction:=wdCollapseEnd
        .Execute FindText:="RECORDS"
        Pos3 = Selection.Start
    End With
    Call FillOneTable(Pos1, Pos2, "PreTbl")
    Call FillOneTable(Pos2, Pos3, "PerfTbl")
    Application.ScreenUpdating = True
End Sub

Sub FillOneTable(Pos1 As Long, Pos2 As Long, bmk As String)
    Dim rng As Range
    Dim arr()
    Dim tmp As String
    Dim r As Long
    Dim c As Long
    Dim n As Long
    Set rng = ActiveDocument.Range(Start:=Pos1, End:=Pos2)
    With rng.Find
        .ClearFormatting
        .Text = "SIGN-OFF"
        Do While .Execute
            If rng.Start >= Pos2 Then Exit Do
            rng.Select
            n = n + 1
            ReDim Preserve arr(1 To 3, 1 To n)
            tmp = Selection.Paragraphs(1).Range.Text
            tmp = Replace(tmp, "SIGN-OFF", "")
            tmp = Replace(tmp, vbCr, "")
            arr(3, n) = Trim(tmp) & "____"
            Do
                Selection.MoveUp Unit:=wdParagraph
            Loop Until Selection.Style Like "Heading*"
            arr(1, n) = Selection.Range.ListFormat.ListString
            tmp = Selection.Paragraphs(1).Range.Text
            tmp = Replace(tmp, vbCr, "")
            arr(2, n) = tmp
        Loop
    End With
    ActiveDocument.Bookmarks(bmk).Select
    For c = 1 To n
        For r = 1 To 3
            Selection.MoveRight Unit:=wdCell
            Selection.TypeText Text:=arr(r, c)
        Next r
    Next c
End Sub
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Build table in Word 2010

Post by Sundog »

Thank you for your extra effort! I will give it a go, this evening if possible.
Sundog

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Build table in Word 2010

Post by HansV »

I'm curious to know if you were able to use the code that I posted almost a week ago.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Build table in Word 2010

Post by Sundog »

Hans, I got shunted onto another project that took me away from the table population issue. I'm back on it intermittently now and will post when I have some results to share. Thank you again for your invaluable help.
Sundog