how to get the starting TOC for the comment text

R

Raju

All

In a word macro, for a given comment, I'd like to find a starting TOC
entry (e.g, if the comment is included in a paragraph under section "1.2
Examples", the macro should return 1.2").

In a loop, I'm printing all comments properties
(ActiveDocument.Comments.Scope, Author etc). Along with that I'd like to
have TOC entry of that section).

Please help

Thanks
Raju
 
S

Stefan Blom

If you are trying to figure out the number of the *current* heading for each
comment, this cannot be done easily. As far as I know, you would have to
start at the paragraph where the comment was inserted and then work
backwards until you encounter a heading style.

However, if you want to get the numbering of a *specific* built-in heading
level, you could make use of a STYLEREF field like this:

Sub GetCurrentHeading1ForComment()
Dim c As Comment
Dim r As Range
For Each c In ActiveDocument.Comments
Set r = c.Range.Duplicate
r.Collapse wdCollapseEnd
Set f = ActiveDocument.Fields.Add(Range:=r, _
Text:="STYLEREF 1 \s")

Debug.Print f.Result.Text 'Printing to the Immediate window
'as an example
f.Delete 'Delete the inserted field again
Next c

End Sub

The example looks for Heading 1, but you could of course change that, by
using STYLEREF 2 for Heading 2, etc. And for a custom heading, you could use
the style name instead.
 

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