Get the number of the last "Heading 1"-Styled Paragraph above the cursor with VBA in Microsoft Word

99 Views Asked by At

For Example,

  1. This is a Heading 1 Paragraph

    5.1. This is a Heading 2 Paragraph

    This is the text.

When the cursor is somewhere in "This is the text", I want to get 5 which is the number of the Heading1-styled paragraph on the top of it.

At the moment, I'm using

Dim myField As Field

Set myField = ActiveDocument.Fields.Add(Selection.Range, Type:=wdFieldEmpty, Text:="STYLEREF  ""Heading 1"" \n ")

       If myField.result = index Then
               ....
       End If

Although myField.result gets the number that I need, it also adds it to the text wherever the cursor is, which I don't want.

I need this number to compare it with an existing index in my code.

How can I do this?

0

There are 0 best solutions below