Solved

Word macro to generate cross-reference table, using headings

Posted on 2013-11-20
2
794 Views
Last Modified: 2013-11-21
Hi there,

I have a bunch of word documents that use outlined, numbered headings each with an appropriately defined style.

For example.

1. Introduction
1.1 Scope

The scope of this document is...

Throughout such a structured document, we have unique identifiers, such as: XXXX-123, XXXX-456.

A sample of how these appear is:

4. Overview
4.1 Part A

Some other para...

Another para...

XXXX-123 Text for this identifier...
XXXX-456 Text for this other identifier.

5. Next section
5.1 Subsection A
5.1.1 Introduction

Para...

Another para...

XXXX-789 Text for this identifier.

Now, basically I want a macro that generates a table such as this:

Col1 - ID, Col2 - Section
XXXX-123, 4.1 Part A
XXXX-456, 4.1 Part A
XXXX-789, 5.1.1 Introduction

Note: page numbers are not really relevant - my goal is to keep track of the identifiers.

The pseudo code I have in my head is:

for each match (identifier expression) in document
   get nearest heading
   write identifier to col1
   write heading to col2
next match

Open in new window


Code should ideally work in Word 2003, Word 2010.

Cheers,

Xav.
0
Comment
Question by:xavier_da
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39666412
Not sure where you are stuck, but this illustrates a way of doing it.

1. Create new document
2. Create the table in the new document
3. Step through the input document paragraph by paragraph
4. If it's not body text, save the paragraph text to a variable
5. If an identifiers is found in the paragraph, create a new table row and fill the row with the text and the saved heading


You'll probably have to tweak it a bit, but here is some code that tries to do the above:
Sub ParseIdentifiers()
    Dim docA As Document, docB As Document
    Dim tbl As Table
    Dim rw As Row
    Dim para As Paragraph
    Dim strCol2Text As String
    
    Set docA = ActiveDocument
    Set docB = Documents.Add
    Set tbl = docB.Tables.Add(docB.Range, 1, 2)
    For Each para In docA.Range.Paragraphs
        If para.OutlineLevel <> wdOutlineLevelBodyText Then
            strCol2Text = para.Range.Text
        End If
        With para.Range.Find
            .Text = "XXXX-[0-9]{3}"
            .MatchWildcards = True
            If .Execute() Then
                Set rw = tbl.Rows.Add
                rw.Cells(1).Range.Text = para.Range.Text
                rw.Cells(2).Range.Text = strCol2Text
            End If
        End With
    Next para
End Sub

Open in new window

0
 

Author Closing Comment

by:xavier_da
ID: 39668010
Thanks very much GrahamSkan!

That does the trick - and I know enough to modify/tweak as you say. For example, to get the heading number, you need to append:

strCol2.Text = para.Range.ListFormat.ListString & " " & para.Range.Text

The other mod I made was in the wildcard search, changing the .Text to be: "XXXX-[0-9]{1,4}" with the last bit allowing matches against XXXX-1 through to XXXX-1234.

Again, thanks for the quick and concise response - well done.

Cheers,

Xav.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article will shed light on the latest trends when it comes to your resume building needs. For far too long, the traditional CV format has monopolized the recruitment market.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question