Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    I am using vba to locate quoted terms within my document. To locate the term, I am using VBA to run a wildcard search for the term within quotes. In certain instances, the quoted term has a heading (see below). I want to define my range so that it starts with the heading and ends with the quoted term (i.e., see below, Annual Rate¶“Annual Rate”). I am able to locate and define my range as the quoted term. Now, how would I extend the range backwards to include the Annual Rate heading? The headings will always be the entire paragraph before the quoted term. Any suggestions?

    Annual Rate

    “Annual Rate” means the interest rate ...

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    There is a way to look at a previous paragraph based on your current Range or Selection. Would you be checking whether the previous paragraph has a "Heading x" style applied, or, how exactly would you know that you wanted to include or exclude it?

    Here's some sample code that might be useful if it's got Heading 1 - 4 style applied:

    Code:
    Sub ExtendRange(rngTemp As Word.Range, Optional blnBackwards As Boolean = True)
    ' Check style of previous or next paragraph and extend the range
    '  if the style matches the style list
    Dim sty As Word.Style, intUnits As Integer
    If blnBackwards Then
        Set sty = rngTemp.Previous.Paragraphs(1).Style
        intUnits = -1
    Else
        Set sty = rngTemp.Next.Paragraphs(1).Style
        intUnits = 1
    End If
    Select Case sty.NameLocal
        Case "Heading 1", "Heading 2", "Heading 3", "Heading 4"
            rngTemp.MoveStart wdParagraph, intUnits
        Case Else
            ' No action
    End Select
    
    Sub TESTExtendRange()
    ' For testing, use a selection in the document
    Dim rng As Word.Range
    Set rng = Selection.Range
    ' Extend the range
    ExtendRange rng
    ' For testing, select the range
    rng.Select
    ' Clean up
    Set rng = Nothing
    End Sub

  3. #3
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    I believe "rngTemp.MoveStart wdParagraph, intUnits" is the line of code I require.

    To answer your question, no the styles are not necessarily consistent in the documents, so using a style to direct the program might cause problems. My program prompts the user when they run the program. If the quoted term is defined as my previous post shows, the program needs to adjust the start of the range to the beginning of the previous paragraph within that article of the document, only.

    Your post was very helpful.

    Thank you very much.

    Larry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •