Solved

Word macro to generate cross-reference table, using headings

Posted on 2013-11-20
2
825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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