Solved

Word macro to generate cross-reference table, using headings

Posted on 2013-11-20
2
756 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now