Results 1 to 9 of 9
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Field / Range (Word 2002)

    I'm using the range object to examine the header(s) in a specified Section. If I find a Page field in the range, I want to delete it, as well as delete the paragraph in which it is located. I would like to set the field as a range, and then expand that range to the paragrah, and then delete. I'm not succeeding at defining the field as a range so that I can expand it to the paragraph. My code follows here... (iSection is the section number I'm working with)

    rng = ActiveDocument.Sections(iSection).Headers(wdHeader FooterPrimary).Range
    For Each fld In rng.Fields
    If fld.Type = wdFieldPage Then
    Set rngPara = fld ' ERROR OCCURS HERE
    rngPara.Expand wdParagraph
    rngPara.Delete
    End If
    Next fld

    Is there a better approach to use here?

    Thanks, Richard Barrett

  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: Field / Range (Word 2002)

    Odd, even though Code is the default property, you can't leave it out. Try

    Set rngPara = fld.Code

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Field / Range (Word 2002)

    No, that didn't work. I did get the results I want by looping through each paragraph in the range; within each paragraph I look for the Page field, and if found, I delete the paragraph.

    Thanks for your help. Richard

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Field / Range (Word 2002)

    Try this way
    <pre> Dim fld As Field, rng As Range
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary).Range
    For Each fld In rng.Fields
    If fld.Type = wdFieldPage Then
    fld.Result.Paragraphs(1).Range.Delete
    End If
    Next fld</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Field / Range (Word 2002)

    Yes, of course. This is the simplest method.

    Thanks.
    Richard

  6. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Field / Range (Word 2002)

    Andrew, Here's something related you might be able to help me with. In the same situation, I set my range as follows to expand to the whole paragraph.

    Set rng = fld.result.Paragraphs(1).Range

    Now I would like to move the end of the range to the point just before the Page field. I want to "read" the text of the paragraph up to the Page field. Generally, the Page field as at the end of the paragraph, and I've moved to the left -(Len(fld.result) + 1) (+1 for the paragraph mark). I've not been successful at simply moving to the left until I've passed the Page field. Can you offer any help?

    Thanks, Richard

  7. #7
    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: Field / Range (Word 2002)

    Length is a bit tricky for fields. Every range should have a .Start property. Can you use that in this case?

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Field / Range (Word 2002)

    Does this give you anything to use?
    <pre> Dim fld As Field, rng As Range, rng2 As Range
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary).Range
    For Each fld In rng.Fields
    If fld.Type = wdFieldPage Then
    Set rng2 = fld.Result.Paragraphs(1).Range
    rng2.End = fld.Result.Start
    MsgBox rng2.text
    End If
    Next fld
    End Sub</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #9
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Field / Range (Word 2002)

    Yes, thanks; that's probably better than what I came up with...

    Dim rng As Range
    Dim f As Field
    Set rng = ActiveDocument.Paragraphs(1).Range
    For Each f In rng.Fields
    If f.Type = wdFieldPage Then
    Set rng = f.Result
    rng.Collapse wdCollapseStart
    rng.Move wdCharacter, -1
    rng.MoveStart wdParagraph, -1
    MsgBox rng.Text
    Exit For
    End If
    Next

Posting Permissions

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