Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2007

Tip: Looking for answers? Try searching our database.

Finding the number of the nearest previous heading level

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave Gapp - 15 Aug 2007 13:54 GMT
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.
Russ - 15 Aug 2007 19:00 GMT
The last topic on this page talks about outline level:
<http://word.mvps.org/faqs/numbering/liststring.htm>

> 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
[quoted text clipped - 7 lines]
> number without reparsing the whole document and effectively re-generating the
> numbers as Word may have to do.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 15 Aug 2007 19:10 GMT
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
[quoted text clipped - 7 lines]
> number without reparsing the whole document and effectively re-generating the
> numbers as Word may have to do.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 16 Aug 2007 06:17 GMT
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
[quoted text clipped - 11 lines]
>> number without reparsing the whole document and effectively re-generating the
>> numbers as Word may have to do.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Dave Gapp - 17 Aug 2007 01:59 GMT
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?
[quoted text clipped - 43 lines]
> >> number without reparsing the whole document and effectively re-generating the
> >> numbers as Word may have to do.
Russ - 17 Aug 2007 07:33 GMT
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
[quoted text clipped - 53 lines]
>>>> the
>>>> numbers as Word may have to do.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Dave Gapp - 19 Aug 2007 11:54 GMT
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

> Dave,
> Could you give an example of what you see now? And an example of what you
[quoted text clipped - 61 lines]
> >>>> the
> >>>> numbers as Word may have to do.
Dave Gapp - 19 Aug 2007 13:24 GMT
After a bit of digging, I found out how to get to the clipboard (see code
below, which simplifies things as I don't need to open another doc to paste
text into...
This bit of code works fine to get the heading number of the start of the
selection

   Dim s As String
   Selection.Collapse (wdCollapseStart)
   Selection.MoveStart Unit:=wdParagraph, Count:=-1
   Selection.MoveEnd Unit:=wdParagraph, Count:=1
   
   Selection.Copy
   s = GetOffClipboard()
   s = Left(s, InStr(s, Chr$(vbKeyTab)))
   MsgBox (s)

Public Function GetOffClipboard() As Variant
'
'   From http://www.cpearson.com/excel/clipboard.htm
'   needs to ensure Tools/References enables the Microsoft Forms 2.0 Library
'       which lives in Fm20.dll under C:\WINDOWS\system32
'
   Dim MyDataObj As New DataObject
   MyDataObj.GetFromClipboard
   GetOffClipboard = MyDataObj.GetText()
End Function

> Russ,
> the documents in which I want to run code are generated by third parties and
[quoted text clipped - 118 lines]
> > >>>> the
> > >>>> numbers as Word may have to do.
Russ - 20 Aug 2007 00:21 GMT
Dave,
Sorry, I didn't realize how hard it is to search for 'real' format list
numbers. Using parts of what I had before and what you discovered, I came up
with this Function to search backwards from selection. It is only looking
for paragraphs that begin with a number. So it would have to be changed to
find any other type of outlining format.

Public Sub TestLastOutlineLevel()
   MsgBox LastOutlineLevel
End Sub

Function LastOutlineLevel()
Dim aNumber As Long
Dim aRange As Word.Range
Dim s As String
Dim myData As DataObject
Set myData = New DataObject
Set aRange = ActiveDocument.Range(0, Selection.Range.End)
For aNumber = aRange.Paragraphs.Count To 1 Step -1
   ActiveDocument.Paragraphs(aNumber).Range.Copy
   myData.GetFromClipboard
   s = myData.GetText(1)
   s = Left(s, InStr(s, vbTab))
   If s Like "#*" Then
       LastOutlineLevel = s
       Exit For
   End If
   s = myData.GetText(1)
   s = Left(s, InStr(s, " "))
   If s Like "#*" Then
       LastOutlineLevel = s
       Exit For
   End If
Next aNumber
End Function

> After a bit of digging, I found out how to get to the clipboard (see code
> below, which simplifies things as I don't need to open another doc to paste
[quoted text clipped - 158 lines]
>>>>>>> the
>>>>>>> numbers as Word may have to do.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.