Finding a “Heading” Style in a Word Document












1















I have a Word macro that allows to put his/her cursor anywhere in a Word document and it finds and saves the Heading 1, Heading 2 and Heading 3 text that is above the text selected by the user in order capture the chapter, section and sub-section that is associated with any sentence in the document.



I am currently using the code below which moves up the document line-by-line until it finds a style that contains "Heading x". When I have completed this task I move down the number of lines that I moved up to get to Heading 1, which may be many pages.



As you can imagine this is awkward, takes a long time (sometimes 60+ seconds) and is visually disturbing.



The code below is that subroutine that identifies the heading.



 Dim str_heading_txt, hdgn_STYLE As String 
Dim SELECTION_PG_NO as Integer
hdng_STYLE = Selection.Style
Do Until Left(hdng_STYLE, 7) = "Heading"
LINESUP = LINESUP + 1
Selection.MoveUp Unit:=wdLine, COUNT:=1
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
hdng_STYLE = Selection.Style
'reached first page without finding heading
SELECTION_PG_NO = Selection.Information(wdActiveEndPageNumber)
If SELECTION_PG_NO = 1 Then 'exit if on first page
a_stop = True
Exit Sub
End If
Loop
str_heading_txt = Selection.Sentences(1)


I tried another approach below in order to eliminate the scrolling and performance issues using the Range.Find command below.



I am having trouble getting the selection range to move to the text with the "Heading 1" style. The code selects the sentence at the initial selection, not the text with the "Heading 1" style.



Ideally the Find command would take me to any style that contained "Heading" but, if required, I can code separately for "Heading 1", "Heading 2" and "Heading 3".



What changes to the code are required so that "Heading 1" is selected or, alternatively, that "Heading" is selected?



Dim str_heading_txt, hdgn_STYLE As String
Dim Rng As Range
Dim Fnd As Boolean

Set Rng = Selection.Range
With Rng.Find
.ClearFormatting
.Style = "Heading 1"
.Forward = False
.Execute

Fnd = .Found
End With

If Fnd = True Then
With Rng
hdng_STYLE = Selection.Style
str_heading_txt = Selection.Sentences(1)
End With
End If


Any assistance is sincerely appreciated.










share|improve this question





























    1















    I have a Word macro that allows to put his/her cursor anywhere in a Word document and it finds and saves the Heading 1, Heading 2 and Heading 3 text that is above the text selected by the user in order capture the chapter, section and sub-section that is associated with any sentence in the document.



    I am currently using the code below which moves up the document line-by-line until it finds a style that contains "Heading x". When I have completed this task I move down the number of lines that I moved up to get to Heading 1, which may be many pages.



    As you can imagine this is awkward, takes a long time (sometimes 60+ seconds) and is visually disturbing.



    The code below is that subroutine that identifies the heading.



     Dim str_heading_txt, hdgn_STYLE As String 
    Dim SELECTION_PG_NO as Integer
    hdng_STYLE = Selection.Style
    Do Until Left(hdng_STYLE, 7) = "Heading"
    LINESUP = LINESUP + 1
    Selection.MoveUp Unit:=wdLine, COUNT:=1
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    hdng_STYLE = Selection.Style
    'reached first page without finding heading
    SELECTION_PG_NO = Selection.Information(wdActiveEndPageNumber)
    If SELECTION_PG_NO = 1 Then 'exit if on first page
    a_stop = True
    Exit Sub
    End If
    Loop
    str_heading_txt = Selection.Sentences(1)


    I tried another approach below in order to eliminate the scrolling and performance issues using the Range.Find command below.



    I am having trouble getting the selection range to move to the text with the "Heading 1" style. The code selects the sentence at the initial selection, not the text with the "Heading 1" style.



    Ideally the Find command would take me to any style that contained "Heading" but, if required, I can code separately for "Heading 1", "Heading 2" and "Heading 3".



    What changes to the code are required so that "Heading 1" is selected or, alternatively, that "Heading" is selected?



    Dim str_heading_txt, hdgn_STYLE As String
    Dim Rng As Range
    Dim Fnd As Boolean

    Set Rng = Selection.Range
    With Rng.Find
    .ClearFormatting
    .Style = "Heading 1"
    .Forward = False
    .Execute

    Fnd = .Found
    End With

    If Fnd = True Then
    With Rng
    hdng_STYLE = Selection.Style
    str_heading_txt = Selection.Sentences(1)
    End With
    End If


    Any assistance is sincerely appreciated.










    share|improve this question



























      1












      1








      1








      I have a Word macro that allows to put his/her cursor anywhere in a Word document and it finds and saves the Heading 1, Heading 2 and Heading 3 text that is above the text selected by the user in order capture the chapter, section and sub-section that is associated with any sentence in the document.



      I am currently using the code below which moves up the document line-by-line until it finds a style that contains "Heading x". When I have completed this task I move down the number of lines that I moved up to get to Heading 1, which may be many pages.



      As you can imagine this is awkward, takes a long time (sometimes 60+ seconds) and is visually disturbing.



      The code below is that subroutine that identifies the heading.



       Dim str_heading_txt, hdgn_STYLE As String 
      Dim SELECTION_PG_NO as Integer
      hdng_STYLE = Selection.Style
      Do Until Left(hdng_STYLE, 7) = "Heading"
      LINESUP = LINESUP + 1
      Selection.MoveUp Unit:=wdLine, COUNT:=1
      Selection.HomeKey Unit:=wdLine
      Selection.EndKey Unit:=wdLine, Extend:=wdExtend
      hdng_STYLE = Selection.Style
      'reached first page without finding heading
      SELECTION_PG_NO = Selection.Information(wdActiveEndPageNumber)
      If SELECTION_PG_NO = 1 Then 'exit if on first page
      a_stop = True
      Exit Sub
      End If
      Loop
      str_heading_txt = Selection.Sentences(1)


      I tried another approach below in order to eliminate the scrolling and performance issues using the Range.Find command below.



      I am having trouble getting the selection range to move to the text with the "Heading 1" style. The code selects the sentence at the initial selection, not the text with the "Heading 1" style.



      Ideally the Find command would take me to any style that contained "Heading" but, if required, I can code separately for "Heading 1", "Heading 2" and "Heading 3".



      What changes to the code are required so that "Heading 1" is selected or, alternatively, that "Heading" is selected?



      Dim str_heading_txt, hdgn_STYLE As String
      Dim Rng As Range
      Dim Fnd As Boolean

      Set Rng = Selection.Range
      With Rng.Find
      .ClearFormatting
      .Style = "Heading 1"
      .Forward = False
      .Execute

      Fnd = .Found
      End With

      If Fnd = True Then
      With Rng
      hdng_STYLE = Selection.Style
      str_heading_txt = Selection.Sentences(1)
      End With
      End If


      Any assistance is sincerely appreciated.










      share|improve this question
















      I have a Word macro that allows to put his/her cursor anywhere in a Word document and it finds and saves the Heading 1, Heading 2 and Heading 3 text that is above the text selected by the user in order capture the chapter, section and sub-section that is associated with any sentence in the document.



      I am currently using the code below which moves up the document line-by-line until it finds a style that contains "Heading x". When I have completed this task I move down the number of lines that I moved up to get to Heading 1, which may be many pages.



      As you can imagine this is awkward, takes a long time (sometimes 60+ seconds) and is visually disturbing.



      The code below is that subroutine that identifies the heading.



       Dim str_heading_txt, hdgn_STYLE As String 
      Dim SELECTION_PG_NO as Integer
      hdng_STYLE = Selection.Style
      Do Until Left(hdng_STYLE, 7) = "Heading"
      LINESUP = LINESUP + 1
      Selection.MoveUp Unit:=wdLine, COUNT:=1
      Selection.HomeKey Unit:=wdLine
      Selection.EndKey Unit:=wdLine, Extend:=wdExtend
      hdng_STYLE = Selection.Style
      'reached first page without finding heading
      SELECTION_PG_NO = Selection.Information(wdActiveEndPageNumber)
      If SELECTION_PG_NO = 1 Then 'exit if on first page
      a_stop = True
      Exit Sub
      End If
      Loop
      str_heading_txt = Selection.Sentences(1)


      I tried another approach below in order to eliminate the scrolling and performance issues using the Range.Find command below.



      I am having trouble getting the selection range to move to the text with the "Heading 1" style. The code selects the sentence at the initial selection, not the text with the "Heading 1" style.



      Ideally the Find command would take me to any style that contained "Heading" but, if required, I can code separately for "Heading 1", "Heading 2" and "Heading 3".



      What changes to the code are required so that "Heading 1" is selected or, alternatively, that "Heading" is selected?



      Dim str_heading_txt, hdgn_STYLE As String
      Dim Rng As Range
      Dim Fnd As Boolean

      Set Rng = Selection.Range
      With Rng.Find
      .ClearFormatting
      .Style = "Heading 1"
      .Forward = False
      .Execute

      Fnd = .Found
      End With

      If Fnd = True Then
      With Rng
      hdng_STYLE = Selection.Style
      str_heading_txt = Selection.Sentences(1)
      End With
      End If


      Any assistance is sincerely appreciated.







      vba ms-word






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 6:27









      Cindy Meister

      14.9k102234




      14.9k102234










      asked Nov 13 '18 at 23:51









      Kaiser OttoKaiser Otto

      296




      296
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can use the range.GoTo() method.



          Dim rngHead As Range, str_heading_txt As String, hdgn_STYLE As String
          Set rngHead = Selection.GoTo(wdGoToHeading, wdGoToPrevious)

          'Grab the entire text - headers are considered a paragraph
          rngHead.Expand wdParagraph

          ' Read the text of your heading
          str_heading_txt = rngHead.Text

          ' Read the style (name) of your heading
          hdgn_STYLE = rngHead.Style


          I noticed that you used Selection.Sentences(1) to grab the text, but headings are already essentially a paragraph by itself - so you can just use the range.Expand() method and expand using wdParagraph





          Also, a bit of advice:



          When declaring variables such as:



          Dim str_heading_txt, hdgn_STYLE As String


          Your intent was good, but str_heading_txt was actually declared as type Variant. Unfortunately with VBA, if you want your variables to have a specific data type, you much declare so individually:



          Dim str_heading_txt As String, hdgn_STYLE As String


          Or some data types even have "Shorthand" methods known as Type Characters:



          Dim str_heading_txt$, hdgn_STYLE$


          Notice how the $ was appended to the end of your variable? This just declared it as a String without requiring the As String.



          Some Common Type-Characters:





          • $ String


          • & Long


          • % Integer


          • ! Single


          • # Double


          You can even append these to the actual value:



          Dim a

          a = 5

          Debug.Print TypeName(a) 'Prints Integer (default)

          a = 5!

          Debug.Print TypeName(a) 'Prints Single





          share|improve this answer


























          • Thanks @K.Davis. Great solution! Great tips. One small point: rndHead.Text gives me the chapter/sub-chapter number (i.e. "2" for chapter 2. My goal is capture the title text, e.g. "Chapter Two".

            – Kaiser Otto
            Nov 14 '18 at 3:54











          • Is that Heading 1 format?

            – K.Dᴀᴠɪs
            Nov 14 '18 at 3:56











          • @KaiserOtto Please don't accept until your answer is fulfilled. I would like to assist in fully answering your question

            – K.Dᴀᴠɪs
            Nov 14 '18 at 4:28











          • Yes. It's a numbered list. So Heading 1 is level 1, i.e. "2" and Heading 2 is level 2, e.g. "2.1" Sub-chapter

            – Kaiser Otto
            Nov 14 '18 at 4:30











          • I just want to reconfirm, because you are saying "Title Text" in your first comment - there's a separate format called Title - which is not Heading 1. Can you please confirm which format exactly you are after? Heading 1 or Title? Thanks!

            – K.Dᴀᴠɪs
            Nov 14 '18 at 4:32





















          0














          Try something based on:



          Sub Demo()
          Dim Rng As Range, StrHd As String, s As Long
          s = 10
          With Selection
          Set Rng = .Range
          Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
          StrHd = Rng.Paragraphs.First.Range.Text
          Do While Right(Rng.Paragraphs.First.Style, 1) > 1
          Rng.End = Rng.Start - 1
          Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
          With Rng.Paragraphs.First
          If Right(.Style, 1) < s Then
          s = Right(.Style, 1)
          StrHd = .Range.Text & StrHd
          End If
          End With
          Loop
          MsgBox StrHd
          End With
          End Sub





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291161%2ffinding-a-heading-style-in-a-word-document%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can use the range.GoTo() method.



            Dim rngHead As Range, str_heading_txt As String, hdgn_STYLE As String
            Set rngHead = Selection.GoTo(wdGoToHeading, wdGoToPrevious)

            'Grab the entire text - headers are considered a paragraph
            rngHead.Expand wdParagraph

            ' Read the text of your heading
            str_heading_txt = rngHead.Text

            ' Read the style (name) of your heading
            hdgn_STYLE = rngHead.Style


            I noticed that you used Selection.Sentences(1) to grab the text, but headings are already essentially a paragraph by itself - so you can just use the range.Expand() method and expand using wdParagraph





            Also, a bit of advice:



            When declaring variables such as:



            Dim str_heading_txt, hdgn_STYLE As String


            Your intent was good, but str_heading_txt was actually declared as type Variant. Unfortunately with VBA, if you want your variables to have a specific data type, you much declare so individually:



            Dim str_heading_txt As String, hdgn_STYLE As String


            Or some data types even have "Shorthand" methods known as Type Characters:



            Dim str_heading_txt$, hdgn_STYLE$


            Notice how the $ was appended to the end of your variable? This just declared it as a String without requiring the As String.



            Some Common Type-Characters:





            • $ String


            • & Long


            • % Integer


            • ! Single


            • # Double


            You can even append these to the actual value:



            Dim a

            a = 5

            Debug.Print TypeName(a) 'Prints Integer (default)

            a = 5!

            Debug.Print TypeName(a) 'Prints Single





            share|improve this answer


























            • Thanks @K.Davis. Great solution! Great tips. One small point: rndHead.Text gives me the chapter/sub-chapter number (i.e. "2" for chapter 2. My goal is capture the title text, e.g. "Chapter Two".

              – Kaiser Otto
              Nov 14 '18 at 3:54











            • Is that Heading 1 format?

              – K.Dᴀᴠɪs
              Nov 14 '18 at 3:56











            • @KaiserOtto Please don't accept until your answer is fulfilled. I would like to assist in fully answering your question

              – K.Dᴀᴠɪs
              Nov 14 '18 at 4:28











            • Yes. It's a numbered list. So Heading 1 is level 1, i.e. "2" and Heading 2 is level 2, e.g. "2.1" Sub-chapter

              – Kaiser Otto
              Nov 14 '18 at 4:30











            • I just want to reconfirm, because you are saying "Title Text" in your first comment - there's a separate format called Title - which is not Heading 1. Can you please confirm which format exactly you are after? Heading 1 or Title? Thanks!

              – K.Dᴀᴠɪs
              Nov 14 '18 at 4:32


















            1














            You can use the range.GoTo() method.



            Dim rngHead As Range, str_heading_txt As String, hdgn_STYLE As String
            Set rngHead = Selection.GoTo(wdGoToHeading, wdGoToPrevious)

            'Grab the entire text - headers are considered a paragraph
            rngHead.Expand wdParagraph

            ' Read the text of your heading
            str_heading_txt = rngHead.Text

            ' Read the style (name) of your heading
            hdgn_STYLE = rngHead.Style


            I noticed that you used Selection.Sentences(1) to grab the text, but headings are already essentially a paragraph by itself - so you can just use the range.Expand() method and expand using wdParagraph





            Also, a bit of advice:



            When declaring variables such as:



            Dim str_heading_txt, hdgn_STYLE As String


            Your intent was good, but str_heading_txt was actually declared as type Variant. Unfortunately with VBA, if you want your variables to have a specific data type, you much declare so individually:



            Dim str_heading_txt As String, hdgn_STYLE As String


            Or some data types even have "Shorthand" methods known as Type Characters:



            Dim str_heading_txt$, hdgn_STYLE$


            Notice how the $ was appended to the end of your variable? This just declared it as a String without requiring the As String.



            Some Common Type-Characters:





            • $ String


            • & Long


            • % Integer


            • ! Single


            • # Double


            You can even append these to the actual value:



            Dim a

            a = 5

            Debug.Print TypeName(a) 'Prints Integer (default)

            a = 5!

            Debug.Print TypeName(a) 'Prints Single





            share|improve this answer


























            • Thanks @K.Davis. Great solution! Great tips. One small point: rndHead.Text gives me the chapter/sub-chapter number (i.e. "2" for chapter 2. My goal is capture the title text, e.g. "Chapter Two".

              – Kaiser Otto
              Nov 14 '18 at 3:54











            • Is that Heading 1 format?

              – K.Dᴀᴠɪs
              Nov 14 '18 at 3:56











            • @KaiserOtto Please don't accept until your answer is fulfilled. I would like to assist in fully answering your question

              – K.Dᴀᴠɪs
              Nov 14 '18 at 4:28











            • Yes. It's a numbered list. So Heading 1 is level 1, i.e. "2" and Heading 2 is level 2, e.g. "2.1" Sub-chapter

              – Kaiser Otto
              Nov 14 '18 at 4:30











            • I just want to reconfirm, because you are saying "Title Text" in your first comment - there's a separate format called Title - which is not Heading 1. Can you please confirm which format exactly you are after? Heading 1 or Title? Thanks!

              – K.Dᴀᴠɪs
              Nov 14 '18 at 4:32
















            1












            1








            1







            You can use the range.GoTo() method.



            Dim rngHead As Range, str_heading_txt As String, hdgn_STYLE As String
            Set rngHead = Selection.GoTo(wdGoToHeading, wdGoToPrevious)

            'Grab the entire text - headers are considered a paragraph
            rngHead.Expand wdParagraph

            ' Read the text of your heading
            str_heading_txt = rngHead.Text

            ' Read the style (name) of your heading
            hdgn_STYLE = rngHead.Style


            I noticed that you used Selection.Sentences(1) to grab the text, but headings are already essentially a paragraph by itself - so you can just use the range.Expand() method and expand using wdParagraph





            Also, a bit of advice:



            When declaring variables such as:



            Dim str_heading_txt, hdgn_STYLE As String


            Your intent was good, but str_heading_txt was actually declared as type Variant. Unfortunately with VBA, if you want your variables to have a specific data type, you much declare so individually:



            Dim str_heading_txt As String, hdgn_STYLE As String


            Or some data types even have "Shorthand" methods known as Type Characters:



            Dim str_heading_txt$, hdgn_STYLE$


            Notice how the $ was appended to the end of your variable? This just declared it as a String without requiring the As String.



            Some Common Type-Characters:





            • $ String


            • & Long


            • % Integer


            • ! Single


            • # Double


            You can even append these to the actual value:



            Dim a

            a = 5

            Debug.Print TypeName(a) 'Prints Integer (default)

            a = 5!

            Debug.Print TypeName(a) 'Prints Single





            share|improve this answer















            You can use the range.GoTo() method.



            Dim rngHead As Range, str_heading_txt As String, hdgn_STYLE As String
            Set rngHead = Selection.GoTo(wdGoToHeading, wdGoToPrevious)

            'Grab the entire text - headers are considered a paragraph
            rngHead.Expand wdParagraph

            ' Read the text of your heading
            str_heading_txt = rngHead.Text

            ' Read the style (name) of your heading
            hdgn_STYLE = rngHead.Style


            I noticed that you used Selection.Sentences(1) to grab the text, but headings are already essentially a paragraph by itself - so you can just use the range.Expand() method and expand using wdParagraph





            Also, a bit of advice:



            When declaring variables such as:



            Dim str_heading_txt, hdgn_STYLE As String


            Your intent was good, but str_heading_txt was actually declared as type Variant. Unfortunately with VBA, if you want your variables to have a specific data type, you much declare so individually:



            Dim str_heading_txt As String, hdgn_STYLE As String


            Or some data types even have "Shorthand" methods known as Type Characters:



            Dim str_heading_txt$, hdgn_STYLE$


            Notice how the $ was appended to the end of your variable? This just declared it as a String without requiring the As String.



            Some Common Type-Characters:





            • $ String


            • & Long


            • % Integer


            • ! Single


            • # Double


            You can even append these to the actual value:



            Dim a

            a = 5

            Debug.Print TypeName(a) 'Prints Integer (default)

            a = 5!

            Debug.Print TypeName(a) 'Prints Single






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 1:16

























            answered Nov 14 '18 at 1:05









            K.DᴀᴠɪsK.Dᴀᴠɪs

            7,189112439




            7,189112439













            • Thanks @K.Davis. Great solution! Great tips. One small point: rndHead.Text gives me the chapter/sub-chapter number (i.e. "2" for chapter 2. My goal is capture the title text, e.g. "Chapter Two".

              – Kaiser Otto
              Nov 14 '18 at 3:54











            • Is that Heading 1 format?

              – K.Dᴀᴠɪs
              Nov 14 '18 at 3:56











            • @KaiserOtto Please don't accept until your answer is fulfilled. I would like to assist in fully answering your question

              – K.Dᴀᴠɪs
              Nov 14 '18 at 4:28











            • Yes. It's a numbered list. So Heading 1 is level 1, i.e. "2" and Heading 2 is level 2, e.g. "2.1" Sub-chapter

              – Kaiser Otto
              Nov 14 '18 at 4:30











            • I just want to reconfirm, because you are saying "Title Text" in your first comment - there's a separate format called Title - which is not Heading 1. Can you please confirm which format exactly you are after? Heading 1 or Title? Thanks!

              – K.Dᴀᴠɪs
              Nov 14 '18 at 4:32





















            • Thanks @K.Davis. Great solution! Great tips. One small point: rndHead.Text gives me the chapter/sub-chapter number (i.e. "2" for chapter 2. My goal is capture the title text, e.g. "Chapter Two".

              – Kaiser Otto
              Nov 14 '18 at 3:54











            • Is that Heading 1 format?

              – K.Dᴀᴠɪs
              Nov 14 '18 at 3:56











            • @KaiserOtto Please don't accept until your answer is fulfilled. I would like to assist in fully answering your question

              – K.Dᴀᴠɪs
              Nov 14 '18 at 4:28











            • Yes. It's a numbered list. So Heading 1 is level 1, i.e. "2" and Heading 2 is level 2, e.g. "2.1" Sub-chapter

              – Kaiser Otto
              Nov 14 '18 at 4:30











            • I just want to reconfirm, because you are saying "Title Text" in your first comment - there's a separate format called Title - which is not Heading 1. Can you please confirm which format exactly you are after? Heading 1 or Title? Thanks!

              – K.Dᴀᴠɪs
              Nov 14 '18 at 4:32



















            Thanks @K.Davis. Great solution! Great tips. One small point: rndHead.Text gives me the chapter/sub-chapter number (i.e. "2" for chapter 2. My goal is capture the title text, e.g. "Chapter Two".

            – Kaiser Otto
            Nov 14 '18 at 3:54





            Thanks @K.Davis. Great solution! Great tips. One small point: rndHead.Text gives me the chapter/sub-chapter number (i.e. "2" for chapter 2. My goal is capture the title text, e.g. "Chapter Two".

            – Kaiser Otto
            Nov 14 '18 at 3:54













            Is that Heading 1 format?

            – K.Dᴀᴠɪs
            Nov 14 '18 at 3:56





            Is that Heading 1 format?

            – K.Dᴀᴠɪs
            Nov 14 '18 at 3:56













            @KaiserOtto Please don't accept until your answer is fulfilled. I would like to assist in fully answering your question

            – K.Dᴀᴠɪs
            Nov 14 '18 at 4:28





            @KaiserOtto Please don't accept until your answer is fulfilled. I would like to assist in fully answering your question

            – K.Dᴀᴠɪs
            Nov 14 '18 at 4:28













            Yes. It's a numbered list. So Heading 1 is level 1, i.e. "2" and Heading 2 is level 2, e.g. "2.1" Sub-chapter

            – Kaiser Otto
            Nov 14 '18 at 4:30





            Yes. It's a numbered list. So Heading 1 is level 1, i.e. "2" and Heading 2 is level 2, e.g. "2.1" Sub-chapter

            – Kaiser Otto
            Nov 14 '18 at 4:30













            I just want to reconfirm, because you are saying "Title Text" in your first comment - there's a separate format called Title - which is not Heading 1. Can you please confirm which format exactly you are after? Heading 1 or Title? Thanks!

            – K.Dᴀᴠɪs
            Nov 14 '18 at 4:32







            I just want to reconfirm, because you are saying "Title Text" in your first comment - there's a separate format called Title - which is not Heading 1. Can you please confirm which format exactly you are after? Heading 1 or Title? Thanks!

            – K.Dᴀᴠɪs
            Nov 14 '18 at 4:32















            0














            Try something based on:



            Sub Demo()
            Dim Rng As Range, StrHd As String, s As Long
            s = 10
            With Selection
            Set Rng = .Range
            Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
            StrHd = Rng.Paragraphs.First.Range.Text
            Do While Right(Rng.Paragraphs.First.Style, 1) > 1
            Rng.End = Rng.Start - 1
            Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
            With Rng.Paragraphs.First
            If Right(.Style, 1) < s Then
            s = Right(.Style, 1)
            StrHd = .Range.Text & StrHd
            End If
            End With
            Loop
            MsgBox StrHd
            End With
            End Sub





            share|improve this answer




























              0














              Try something based on:



              Sub Demo()
              Dim Rng As Range, StrHd As String, s As Long
              s = 10
              With Selection
              Set Rng = .Range
              Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
              StrHd = Rng.Paragraphs.First.Range.Text
              Do While Right(Rng.Paragraphs.First.Style, 1) > 1
              Rng.End = Rng.Start - 1
              Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
              With Rng.Paragraphs.First
              If Right(.Style, 1) < s Then
              s = Right(.Style, 1)
              StrHd = .Range.Text & StrHd
              End If
              End With
              Loop
              MsgBox StrHd
              End With
              End Sub





              share|improve this answer


























                0












                0








                0







                Try something based on:



                Sub Demo()
                Dim Rng As Range, StrHd As String, s As Long
                s = 10
                With Selection
                Set Rng = .Range
                Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
                StrHd = Rng.Paragraphs.First.Range.Text
                Do While Right(Rng.Paragraphs.First.Style, 1) > 1
                Rng.End = Rng.Start - 1
                Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
                With Rng.Paragraphs.First
                If Right(.Style, 1) < s Then
                s = Right(.Style, 1)
                StrHd = .Range.Text & StrHd
                End If
                End With
                Loop
                MsgBox StrHd
                End With
                End Sub





                share|improve this answer













                Try something based on:



                Sub Demo()
                Dim Rng As Range, StrHd As String, s As Long
                s = 10
                With Selection
                Set Rng = .Range
                Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
                StrHd = Rng.Paragraphs.First.Range.Text
                Do While Right(Rng.Paragraphs.First.Style, 1) > 1
                Rng.End = Rng.Start - 1
                Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
                With Rng.Paragraphs.First
                If Right(.Style, 1) < s Then
                s = Right(.Style, 1)
                StrHd = .Range.Text & StrHd
                End If
                End With
                Loop
                MsgBox StrHd
                End With
                End Sub






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 9:34









                macropodmacropod

                2,517239




                2,517239






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291161%2ffinding-a-heading-style-in-a-word-document%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma