none
how to get the document's title RRS feed

  • Question

  • i have a big document, how can i get the document's title character quickly?

    for example ,in upper picture, i want to get "MyTitle" text.

    now i iterate all of the paragraphs, and compare the style. below is my code

      public static bool IsTitle(Word.Paragraph p) {
                Word.Style style = (Word.Style)p.get_Style();
                if (style.NameLocal.StartsWith("Title") && 
                    style.ListLevelNumber == 0) {
                    return true;
                }
                return false;
            }

    but iterate the Paragraphs is not efficent way, is any other method?


    • Edited by sendreams Monday, March 3, 2014 1:43 PM
    Monday, March 3, 2014 1:41 PM

Answers

  • You could used Word's Find function to look for a paragraph in the 'Title' Style, then return that paragraph's text. Unless your title text is in the document's first paragraph, that should be much faster than iterating through paragraphs. For example, in VBA:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .Style = "Title"
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      If .Find.Found = True Then MsgBox .Paragraphs(1).Range.Text
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by sendreams Tuesday, March 4, 2014 8:01 AM
    Tuesday, March 4, 2014 7:30 AM
  • The easiest way to get all headings is to insert a Table of Contents and copy that. Alternatively, see the code I posted in: http://social.msdn.microsoft.com/Forums/office/en-US/f0640846-d44d-400e-94ad-e737f0e30b84/refer-to-a-word-document-in-excel?forum=worddev. Although it's for a different purpose, the same principles can be used.

    PS: Instead of resurrecting solved threads for different issues, you should start a new thread.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by sendreams Tuesday, March 11, 2014 8:04 AM
    Tuesday, March 11, 2014 7:42 AM

All replies

  • You could used Word's Find function to look for a paragraph in the 'Title' Style, then return that paragraph's text. Unless your title text is in the document's first paragraph, that should be much faster than iterating through paragraphs. For example, in VBA:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .Style = "Title"
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      If .Find.Found = True Then MsgBox .Paragraphs(1).Range.Text
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by sendreams Tuesday, March 4, 2014 8:01 AM
    Tuesday, March 4, 2014 7:30 AM
  • hi, macropod

    i got it, thank you very much.


    Tuesday, March 4, 2014 8:01 AM
  • hi, macropod

    how can i quickly get all headings?

    if base on this code, only can find one heading style each time.

    the docuemnt.listparagraphs may be a choice, but this collection may be contain hurge number bullet.

    is any idea for it?

    thanks

    Tuesday, March 11, 2014 7:37 AM
  • The easiest way to get all headings is to insert a Table of Contents and copy that. Alternatively, see the code I posted in: http://social.msdn.microsoft.com/Forums/office/en-US/f0640846-d44d-400e-94ad-e737f0e30b84/refer-to-a-word-document-in-excel?forum=worddev. Although it's for a different purpose, the same principles can be used.

    PS: Instead of resurrecting solved threads for different issues, you should start a new thread.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by sendreams Tuesday, March 11, 2014 8:04 AM
    Tuesday, March 11, 2014 7:42 AM
  • some additional info: and i do't want to search serveral times, because i need the headings real sequence.
    Tuesday, March 11, 2014 7:43 AM
  • hi, macropod

    thanks a lot

    your answer is so quickly that i don't even post additonal information. i think the toc solution will be fit for me.

    thank you for your advice.

    Tuesday, March 11, 2014 8:02 AM
  • hi, macropod

    i make a new thread, can you please help me,

    the link is:

    how can quickly get all of headings?

    Tuesday, March 11, 2014 8:57 AM