Results 1 to 8 of 8
  1. #1
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post

    Word range (VBA Word 2003)

    I am attempting to construct a program to locate all paragraphs in an active document that contain (1) tab as the first word of the paragraph and applying a style -- Heading1-- to any paragraph that meets that test. I assumed that the tab would define the end of the word but that is not the case. If I search for the first word, for example "The " the program works correctly. Does anyone have any suggestions? I'd prefer not to run Search/Replace for all instances of (1) tab to something unique, run the program to apply the styles, and then run another search/replace to reconstruct those changed paragraphs.

    Sub ApplyStyles()
    '
    On Error GoTo EarlyExit
    Dim aPara As Paragraph
    Dim aChr As Characters
    Dim aNum As Integer

    For Each aPara In ActiveDocument.Paragraphs
    Select Case aPara.Range.Words(1)
    Case "(1)" & Asc(9) 'asc(9) = tab or vbtab
    aPara.Style = wdStyleHeading1
    Case Else
    aPara.Style = wdStyleBodyText
    End Select
    Next
    Exit Sub

    EarlyExit:
    MsgBox "error in macro"
    End Sub

  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

    Re: Word range (VBA Word 2003)

    How about comparing the first four characters of the paragraph?

    If the numbering is Word's automatic number, then this approach probably isn't going to work...

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word range (VBA Word 2003)

    I think I'd examine each paragraph, obtain the string of that paragraph, use the Instr function to locate the first tab in that paragraph, and then inspect the characters that appear to the left of that tab.

    If you find the leading character is "(" and then if the leading 3 characters are "(1)" you've practically won.
    If you find a field, you will need to test its value for something.
    As Jefferson has pointed out, numbering can throw you off. I've seen several threads here and elsewhere about the numbering ways of means that differ between versions of Word.

  4. #4
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Word range (VBA Word 2003)

    Thanks for your help. I've made progress, please see below. Now, my next task is to determine a method for evaluating whether or not a para number is an (i) (as in (h), (i), (j), (k)) or (i) (as in (i), (ii), (iii)). FYI, this is for newly reformatted documents, so the only need for extracting the headings style numbers is to determine the users designed scheme when first setting up their document, which I might get to at a later date.

    This approach uses the paragraphs collection and I'm not sure of its limitations. That is, can you evaluate context of the paragraph that you are evaluating. So, I'm evaluating the current paragraph, can I look at the previous paragraph and the next paragraph to determine if my paragraph numbers are part of an alpha sequence or a romanette sequences? Thank you in advance. Larry

    Sub ApplyHeadingStyles()
    '

    On Error GoTo EarlyExit
    Dim aPara As Paragraph
    Dim aChr As Characters
    Dim aNum As Integer
    Dim sString As String, sString2 As String


    'message setup your heading styles before you begin

    'pull in heading styles from active scheme


    For Each aPara In ActiveDocument.Paragraphs
    Select Case aPara.Range.Words(1)
    Case "ARTICLE "
    aPara.Style = wdStyleHeading1
    Case "Article "
    aPara.Style = wdStyleHeading1
    Case "SECTION "
    aPara.Style = wdStyleHeading2
    Case "Section "
    aPara.Style = wdStyleHeading2
    Case Else
    With aPara.Range
    If .Characters(1) = "(" Then
    '
    'HEADING3
    'use asc numbers for this a-z = asc nos 97-122
    '
    If Asc(.Characters(2).Text) > 96 Then
    If Asc(.Characters(2).Text) < 123 Then
    aPara.Style = wdStyleHeading3
    End If
    End If
    '
    ' WHAT ABOUT ROMANETTES? (i), (ii), (ix), etc.
    ' [THINK ABOUT THIS ONE]
    ' i, v, x, m = asc(i =105, v=118, x =120)
    ' If .Characters(2) > Asc(96) Then
    ' If .Characters(2) < Asc(123) Then
    ' aPara.Style = wdStyleHeading5
    ' End If
    ' End If
    '
    'HEADING 7
    'A-Z = Asc No. 65-90
    If Asc(.Characters(2).Text) > 64 Then
    If Asc(.Characters(2).Text) < 91 Then
    aPara.Style = wdStyleHeading7
    End If
    End If
    'HEADING 9
    '1-9 = Asc No. 49-57
    If Asc(.Characters(2).Text) > 48 Then
    If Asc(.Characters(2).Text) < 58 Then
    aPara.Style = wdStyleHeading9
    End If
    End If
    End If
    'aPara.Style = wdStyleBodyText
    End With
    End Select
    Next
    Exit Sub

    EarlyExit:
    MsgBox "error in macro"
    End Sub

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Word range (VBA Word 2003)

    If the numbers have been created as a numbered list then you could look at <code>aPara.Range.ListFormat.ListValue</code>, which would be 1 if the i is a number from the sequence i, ii, iii, and 9 if the i is from the alphabetic list a,b,c etc.

    If the numbers have been created manually then you will have to look at the characters at the beginning of the previous and next paragraphs. A paragraph has .Next and .Previous methods that return the adjacent paragraphs that you might find useful here.

    StuartR

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word range (VBA Word 2003)

    Has the numbering been typed in manually? If Word's Bullets and Numbering has been used, you'll have to use a different method.

    You can use the Previous and Next methods of the Paragraph object to obtain the previous and next paragraphs. For example:

    Dim PrevPara As Paragraph
    Dim NextPara As Paragraph
    ...
    For Each aPara In ActiveDocument.Paragraphs
    Set PrevPara = aPara.Previous
    Set NextPara = aPara.Next
    ...
    Next aPara

    Note: when aPara is the first paragraph in the document, PrevPara will be Nothing, and when aPara is the last paragraph in the document, NextPara will be nothing. You will have to test for this, e.g.

    If Not PrevPara Is Nothing Then
    ...
    End If

  7. #7
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Word range (VBA Word 2003)

    Wonderful, thank you for your assistance.
    Larry

  8. #8
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Word range (VBA Word 2003)

    Thank you very much. You've been very helpful.
    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
  •