Generating Table of contents through a macro (VBA)

V

Vipul Patel

Hello,

I need to generate a specialized Table of Contents, thru a macro. The
functionality is as under:

I select a text. Invoke the macro thru a shortcut key. The macro will call
VBA logic which will generate a specialized TOC containing entry of the text
selected.

I have no experience in TOC generation. From where should I start?
 
J

Jezebel

You'll need to say what you mean by 'specialized'. It also doesn't make much
sense to create a TOC for a single piece of text. What are you actually
trying to do?
 
V

Vipul Patel

I have a table in Word. Select rows of this table need to appear in the
specialized TOC. Hope this clarifies what I mean by specialized.
 
J

Jay Freedman

Hi Vipul,

You don't need a macro for this.

Define a paragraph style, which can be formatted the same as Normal
style or can have whatever special formatting you need for the table
text. Apply this style only to the rows you want in the Table of
Contents. Leave the other rows in Normal style or some other style.

In the Insert > Reference > Index & Tables > Table of Contents dialog,
click the Options button. In the next dialog, delete the numbers next
to Heading 1, Heading 2, and Heading 3, and clear the checkbox for
"Outline levels". Type the number 1 in the box next to the name of
your special style.

The TOC will now display only the text formatted with the special
style.

You can have another TOC in the same document that has the regular
settings; the two tables will be independent.
 
V

Vipul Patel

Hello Jay,

Thats exactly what I want to automate. Selecting the text which I want in
the TOC. I should invoke the macro, this macro will see whether this entry
exsts in the specialized TOC, if not, it will make an entry. Can you suggest
me the VBA automation links to which I should refer?

Regards
Vipul Patel (C# MVP)
 
V

Vipul Patel

Hello Jay,

Below is the code which I managed to write for my case:

What I desired is that a new TOC (specialized) be created of the style
"TCTOC" and that contain only those entries of the style "TCTOC". Also I want
this specialized TOC to be in the middle of the document and not at the
start. Any suggestion as to how to place it there. I have a table before
which this TOC needs to be placed. (I do have a general TOC at the start of
the document).

The code does the following:
a. Checks if the "TCTOC" style is present in the document or not. If not, it
is created.
b. The currently selected text is then styled to "TCTOC".
c. Checks if "TCTOC" Table of contents is present in the document, by
Iterating thru TableOfContents collection. If not, "TCTOC" table of content
is created and placed at a pre-determined location.

I need help in the following:
c. Placing the "TCTOC" at a known location in the document.

Can you please advise ? THanks a lot for all the help you have provided till
date,


Dim rngTemp As Range
Dim TCStyle As Style
Dim styPresent As Boolean
Dim TOCPresent As Boolean
Dim TCTOC As TableOfContents
Dim styIndex As Integer



Sub Test0()
Set rngTemp = ActiveDocument.Range(Selection.Start, Selection.End)
styPresent = False
styIndex = 0
Dim myStyleIndex As Integer
For Each sty In ActiveDocument.Styles
If sty.NameLocal = "TCTOC" Then
styPresent = True
myStyleIndex = styIndex
End If
styIndex = styIndex + 1
Next sty

If styPresent = False Then
Set TCStyle = ActiveDocument.Styles.Add(Name:="TCTOC",
Type:=wdStyleTypeCharacter)
With myStyle.Font
.Bold = True
.Italic = True
.Name = "Arial"
.Size = 12
End With
End If
rngTemp.Style = "TCTOC"

For Each toc In ActiveDocument.TablesOfContents
If toc.TableID = "TCTOC" Then TOCPresent = True

Next toc



Dim TOCRange As Range

Set TOCRange = ActiveDocument.Range(ActiveDocument.Content.Start, End:=0)


If TOCPresent = False Then
Set TCTOC = ActiveDocument.TablesOfContents.Add(Range:=TOCRange,
UseFields:=False, TableID:="TCTOC", UseHeadingStyles:=True)
End If

ActiveDocument.TablesOfContents(1).Update

End Sub



Regards
Vipul
 
J

Jay Freedman

Hi Vipul,

The location of the TOC is easy to do -- you include a bookmark in the
template so it also appears in the document, and then you set the
location in the code with

Set TOCRange = ActiveDocument.Bookmarks("TOC_here").Range

instead of the line you have.

There are much more serious problems, though. The main one is the
assumption that you can use a character style to mark a part of a line
for inclusion. It won't work. In order to set the TOC options as I
described in my first reply (and note that I did say there "paragraph
style") in a macro, you need a line like

TCTOC.HeadingStyles.Add Style:="TCTOC", Level:=1

If you try this after defining "TCTOC" as a character style, you'll
get a runtime error 5857, "Character styles are not allowed in Table
of Contents".

You could work around this by redesigning the macro so that, instead
of applying a style, it creates a TC field containing the text of the
selection; and instead of being based on styles, the TOC is based on
TC fields (in the TablesOfContents.Add statement, set UseFields:=True
and UseHeadingStyles:=False).

Another problem is that the TableID property of the TOC is not in any
sense the "name" of that particular TOC, as you think. It identifies a
group of TC fields that will be recognized by that TOC field. It
corresponds to the \f switch in the field code. Furthermore, TableID
can't be more than one letter. Therefore, in your code TOCPresent will
always be False and the code will always add another TOC, even when
one is already present.

Read the VBA help topic about the TableOfContents object and the topic
about TOC field codes to learn about this. In any case, there is no
built-in "name" or "tag" for a TOC field -- you may be able to use the
bookmark for this purpose.

Other, less important considerations:

- You don't want to update ActiveDocument.TablesOfContents(1), which
is always the first TOC in the document. Instead, you want
TCTOC.Update.

- The variables styIndex and myStyleIndex serve no purpose, and all
statements that mention them can be deleted.

- The Dim statements for the variables should all come after the Sub
statement, not before it, unless there's a reason to declare them as
module-level global variables (available in all macros within the same
module).

- There are several cases where you've gotten the names of variables
wrong or confused them with other, similar variable names. One thing
that will help is to put the statement Option Explicit at the top of
the module (see
http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm and
http://www.word.mvps.org/FAQs/MacrosVBA/MaintainableCode.htm). For
example, after you add the style and assign it to the variable
TCStyle, you then have the statement With myStyle.Font -- that should
be With TCStyle.Font.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top