Solved

Word macro to generate cross-reference table, using headings

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

As freelancing is becoming more and more common in the tech industry, certain obstacles are proving to be a challenge to those who are used to more traditional, structured employment. This article is meant to help identify such obstacles and offer a…
A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

759 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

23 Experts available now in Live!

Get 1:1 Help Now