Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Field Properties (XP)

    In some wicked VBA code I'm writing for Word, I want to get all of the text that's on the same line as a particular field. The catch is that I don't want to actually SELECT the field; I just want to loop through the FIELDS collection in a document and display the text that surrounds each field in a dialog box. The problem is that the FIELD object doesn't have a RANGE property. Can anyone think of a workaround? I'm prepared to select the field (to return a SELECTION object) if I have to, and then modify the selection range, but that's not the most elegant way to do it.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

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

    Re: Field Properties (XP)

    This function will return the text on the same line as a given range.

    Private Function GetLine(inRange As Range) As String
    Dim rngWord As Range
    Dim strLine As String
    For Each rngWord In inRange.Paragraphs(1).Range.Words
    If rngWord.Information(wdFirstCharacterLineNumber) = _
    inRange.Information(wdFirstCharacterLineNumber) Then
    strLine = strLine & rngWord.Text
    End If
    Next rngWord
    GetLine = strLine
    End Function

    Here is a quick sub to test it. You would call it using Field(n).Result as the parameter.

    Public Sub testit()
    MsgBox GetLine(Selection.Range)
    End Sub

    StuartR

    Edited to add
    I've just noted your comment that a Field doesn't have a range property, you should use .Result to return the range for a field. This may have been all you were asking for.

  3. #3
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Field Properties (XP)

    Excellent information, thank you.
    In the end, I utilized the bit of information that the .RESULT property is a range; that piece of information ALONE was priceless. (I didn't end up using the subroutine simply because the document had over 85 bookmarks x 15 word-ranges to parse and concantenate; the dialog box took too long to display.)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

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