Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Word macro to generate cross-reference table, using headings

Posted on 2013-11-20
2
Medium Priority
?
860 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
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 …
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…

610 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