Russ,
the documents in which I want to run code are generated by third parties and
could be whatever they use. Typically they use automatic heading numbering,
and often use heading numbering for text as well (ie to get legal numbered
paragraphs). See examples below.
ie typical situation 1:
1.2 Heading level 2
1.2.1 Heading level 3
1.2.1.1 Text para 1, but numbered as an outline level
1.2.1.2 Text para 2, but numbered as an outline level **example comment **
1.2.1.3 Text para 3, but numbered as an outline level
Perhaps unnumbered text or figure may be interspersed.
1.2.1.4 Text para 4, but numbered as an outline level
ie typical situation 2:
1.2 Heading level 2
1.2.1 Heading level 3
Text para 1 unnumbered
Text para 2 unnumbered **example comment **
Text para 3 unnumbered
Perhaps a figure may be interspersed.
Text para 4 unnumbered
These documents may be delivered to us (the customer) and we often generate
comments in Word. However, comments are stored on a separate database and
come from many sources, but are expected in a standard format that references
the paragraph (among other things) in the document. I have generated code to
visit each comment and extract some of the necessary data (eg page number,
comment details, contextual text) but am trying to extract the paragraph
number. My ideas to do this so far are to:
A. If the current para is an outline level:
(i) Copy the para and paste as unformatted text into a new window. I note
that this converts the para number into text. Grab the first numbers in this
new buffer as the para number (probably up to a tab which seems to follow).
(ii) use this number as a reference. In situation 1 above returns "1.2.1.2"
B. If the current para is not outline level:
(i) move to the first previous para that is an outline level
(ii) count the paras to the previous outline
(iii) now get the para number as for A.
(iv) provide the count of paras from the previous outline and the previous
outline number as a reference. In situation 2 above returns "1.2.1.1
paragraph 2"
Given that when Word pastes the heading text as unformatted text into
another buffer it knows the para number, I thought that there should be some
easier way to get hold of it....
I'd just like to say that your suggestion in your post re extracting the
number at the start of a para started me down this direction of thinking...
any improvements on my ideas are most welcome
cheers, dg
Russ said:
Dave,
Could you give an example of what you see now? And an example of what you
don't wand to see?
Are they Word field codes? (SEQ?) ALT/F9 toggles field codes on and off.
What do you see in Print Preview?
Why be concerned about regenerating the heading number? Like you said when
printed it should normally update. Fields can be unlinked or locked.
Thanks for the response Russ but unfortunately, it's not quite what I was
after. I want to be able to return the automatically generated heading
number, ie as the reader would see in the document when printed (as a
reference to someone without the Word version of the document). I suspect
that I may have to do something along the lines you suggested but regenerate
the heading number just as word might.
:
Dave,
Did you want something like this subroutine?
From selection backwards to beginning of document, it locates the previous
paragraph outline level that is not plain body text. (= 10)
It then searches that paragraph for a number with or without a dot or
hyphen.
Public Sub FindPreviousOutlineLevel()
Dim aNumber As Long
Dim aRange As Word.Range
Set aRange = ActiveDocument.Range(0, Selection.Range.End)
For aNumber = aRange.Paragraphs.Count To 1 Step -1
If ActiveDocument.Paragraphs(aNumber).Range.ParagraphFormat.OutlineLevel _
<> 10 Then
Set aRange = ActiveDocument.Paragraphs(aNumber).Range
With aRange.Find
.MatchWildcards = True
.Text = "<[0-9.-]{1,}"
.Execute
If .Found Then
MsgBox aRange ' Number
Else
MsgBox "No Number Found Here: " & aRange
End If
End With
Exit For
End If
Next aNumber
End Sub
Oh, you saw that already. Have you tried to record a macro while manually
inserting a crossreference to see what kind of code it produces for a
heading type.
I am using Word 2003 and I need to be able to find out the heading number
of
the nearest previous heading level in a document (for cross referencing
comments on a document, for example so I can extract that the comment is on
the 3rd para in section 4.6). I have found the para number on the section,
eg via
http://www.word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm
& I could find the outline level of a heading, eg via
http://word.mvps.org/FAQs/Numbering/ListString.htm
but I seem unable to find out the heading number. I understand that Word
may
only generate this when printing, but is there any way to get the heading
number without reparsing the whole document and effectively re-generating
the
numbers as Word may have to do.