PDA

View Full Version : extracting information from the above headline



marcelma
10-09-2010, 03:02 PM
hello,

is there any more elegant way in Word-VBA for extracting needed information from the previous headline of a certain level than setting a bookmark, searching for the headline format and jumping back to the bookmark?

thanks a lot in advance,
Marcel

macropod
10-09-2010, 03:17 PM
Hi Marcel,

If you're using Heading Styles, you can simply insert a cross-reference to the Heading. Word will create the required (hidden) bookmark on the fly.

marcelma
10-09-2010, 03:31 PM
Thanks for the quick reply. It is a good suggestion, but in the present case the text already exists and I need to write a macro which can find the right information and extract it for further processing, rather than doing it all manually.

Yes, I am using Heading Styles.

macropod
10-09-2010, 08:09 PM
Hi Marcel,

If your headings use list level numbering, here's one way of programatically inserting a cross-reference to the last preceding list level:

Sub Test()
Dim i As Integer, xRefs As Variant, rngTmp As Range, rngHead As Range
With Selection
Set rngHead = ActiveDocument.Range(0, 0)
Set rngTmp = ActiveDocument.Range(0, 0)
rngTmp.End = .Paragraphs(1).Range.Start - 1
With rngTmp
For i = 1 To .ListParagraphs.Count
With .ListParagraphs(i)
If .Range.ListFormat.ListString >= rngHead.ListFormat.ListString Then
Set rngHead = .Range
rngHead.End = rngHead.End - 1
End If
End With
Next
End With
xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)
For i = 1 To UBound(xRefs)
If Left(Trim(xRefs(i)), 75) = Left(rngHead.ListFormat.ListString & " " & rngHead.Text, 75) Then
.InsertCrossReference ReferenceType:=wdRefTypeNumberedItem, ReferenceKind:=wdNumberRelativeContext, _
ReferenceItem:=i, InsertAsHyperlink:=True, IncludePosition:=False
End If
Next
End With
Set rngTmp = Nothing: Set rngHead = Nothing
End SubPresumably the approach could be adapted to work with Heading Styles instead.

macropod
10-09-2010, 11:11 PM
Hi Marcel,

Here's the kind of adaptation I had in mind:

Sub CrossRefPrevHead()
Dim i As Integer, xRefs As Variant, rngTmp As Range, rngHead As Range
With Selection
Set rngHead = ActiveDocument.Range(0, 0)
Set rngTmp = ActiveDocument.Range(0, 0)
rngTmp.End = .Paragraphs(1).Range.Start - 1
With rngTmp
For i = 1 To .Paragraphs.Count
With .Paragraphs(i)
If InStr(LCase(.Style), "heading") > 0 Then
Set rngHead = .Range
rngHead.End = rngHead.End - 1
End If
End With
Next
End With
xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
For i = 1 To UBound(xRefs)
If Left(Trim(xRefs(i)), 75) = Left(Trim(rngHead.ListFormat.ListString & " " & rngHead.Text), 75) Then
.InsertCrossReference ReferenceType:=wdRefTypeHeading, ReferenceKind:=wdContentText, _
ReferenceItem:=i, InsertAsHyperlink:=True, IncludePosition:=False
End If
Next
End With
Set rngTmp = Nothing: Set rngHead = Nothing
End SubThis one gets the previous Heading's text.

Paul_Hossler
10-10-2010, 06:45 AM
If you just want the Heading text to be inserted in your document and be automatically updated, you can use the STYLEREF field

Paul