none
how can i scroll to a paragraph RRS feed

  • Question

  • hi

    i have a document which heading like this(without ListString):

    chapter 1

      test1

      test2

    chapter 2

      test1

      test2

    ..

    i want to scroll to "test1" in "chapter 2" by program, but "test1" appeared multi times in document, is any diff between them?

    now i use this method 

    private void ScrollToHeading(string name, string listLabel){
    
    }

    the ListString property is useful but it depend on listTemplate, if in used listTemplate does not have NumberFormat setting, the listString will be empty.

    i don't know how to scroll precisely. is any other property can be use for this situation? thank.

    Friday, January 3, 2014 9:09 AM

Answers

  • Assuming your Chapter headings are in a different Style from everything else, you could use Find to locate each instance, updating a counter each time. When you get to the desired count, exit the Find then select the next paragraph. In VBA, for Chapters using the "Heading 1" Style, to scroll to the first paragraph after the second Chapter heading:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .Style = "Heading 1"
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        i = i + 1
        .Collapse wdCollapseEnd
        If i = 2 Then
          .Select
          Exit Do
        End If
        .Find.Execute
      Loop
      Selection.MoveEnd wdParagraph, 1
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by sendreams Friday, January 3, 2014 11:31 AM
    Friday, January 3, 2014 10:56 AM

All replies

  • Assuming your Chapter headings are in a different Style from everything else, you could use Find to locate each instance, updating a counter each time. When you get to the desired count, exit the Find then select the next paragraph. In VBA, for Chapters using the "Heading 1" Style, to scroll to the first paragraph after the second Chapter heading:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .Style = "Heading 1"
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        i = i + 1
        .Collapse wdCollapseEnd
        If i = 2 Then
          .Select
          Exit Do
        End If
        .Find.Execute
      Loop
      Selection.MoveEnd wdParagraph, 1
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by sendreams Friday, January 3, 2014 11:31 AM
    Friday, January 3, 2014 10:56 AM
  • hi, macropod

    i got it, thank you very much.


    Friday, January 3, 2014 11:32 AM