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

    obtain the Range of a field? (Word97SR2/vba)

    How does one obtain the [search]"Range of a field"[/search[?

    I thought it would be simple, it appears not to be.

    I can loop through the Fields collection, starting at the end and working my way towards the front, identifying fields to be replace, selecting the field and replacing the selection, but I feel I ought to be able to identify the Range of the field ("<font color=448800>Ranges good</font color=448800>; <font color=red>Selection bad</font color=red>") and replace that range's text.

    In the little sample below, not one of the four commented statements will compile syntax free. I'm mightily puzzled.


    <pre>Sub FieldRange()
    Dim rng As Range
    ' Set rng = ActiveDocument.Fields(1).Range
    ' Set rng = ActiveDocument.Fields(1).Code.Range
    ' Set rng = ActiveDocument.Fields(1).Data.Range
    ' Set rng = ActiveDocument.Fields(1).Result.Range
    rng.Text = "new text"
    End Sub

    </pre>


  2. #2
    Lounger
    Join Date
    May 2001
    Location
    the Netherlands
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: obtain the Range of a field? (Word97SR2/vba)

    Hi Chris,

    The "Result" property returns a Range object, so leaving out the "Range" bit:

    Sub FieldRange()
    Dim rng As Range
    Set rng = ActiveDocument.Fields(1).Result
    rng.Text = "new text"
    End Sub

    Same goes for the Code property.

    Changing the Text of the rng created with Result of course only needs an update of the field to change the "new text" bit back to the result of the field's underlying code. Unlinking the field after rng.Text = "new text" would mean replacing the field by text.

    Ingrid

  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: obtain the Range of a field? (Word97SR2/vba)

    The "Result" property returns a Range object, so leaving out the "Range" bit:


    Hmmm. When I run the code below, I get a run-time error '5891' "That Property Is Not Available On That Object".


    <pre> Dim iFlds As Integer
    ' Loop backwards so as not to lose track of existing fields.
    For iFlds = doc.Fields.Count To 1 Step -1
    Dim strCode As String
    strCode = doc.Fields(iFlds).Code
    ' if this is a code for which we search then ...
    If UCase(Left$(strCode, Len(strSearchCode))) = strSearchCode Then
    ' try Ingrid's
    Dim rng As Range
    ' doc is the ActiveDocument, so doc.Fields is a collection of Fields
    ' so doc.Fields(iFlds) is a Field object, whose result returns a Range?!!!
    Set rng = doc.Fields(iFlds).Result ' generates '5891'
    rng.Text = strAr(intcReplaceText, iAr)
    End If
    Next iFlds
    </pre>



    I'm running this code on an ActiveDocument that is a WP5.1 doc, with a single field {Private}.

    From VBA Help: "Field object: Returns a Range object that represents a field's result. You can access a field result without changing the view from field codes. Read/write."

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

    Re: obtain the Range of a field? (Word97SR2/vba)

    The "Result" property returns a Range object, so leaving out the "Range" bit:

    This seems to work well, albeit it's a slow loop around "While .Find.Execute(Replace:=wdReplaceOne)"

    The .Execute of the Find/replace object resets the range, so I have used a range variable (rng) and continually adjust the start point (rng.Start = rng.Start + 1) (yes, I know, that +1 should be +Len())!), and that technique also solves the age-old problem of an infinite loop at the end of the document whenever I'm replacing two consecutive paragraph marks (^p^p) with one (^p).


    So, I can now find/replace text, apply a style, avoid the infinite loop AND return a result that tells me that something happened while I was in there.

    Which is what I need for now.

    Maybe as I learn more I'll discover a better way to do it.



    <pre>Public Function blnRuleText(doc As Document, strAr() As String, iAr As Integer) As Boolean
    ' Given an open document, and a row of a rules array, effect the text find-and-replace

    blnRuleText = False ' default result is failure - "did not find"

    Dim rng As Range ' we will adjust (shrink) this range as we go
    Set rng = Documents(doc).Range
    With rng

    Dim lngStart As Long
    Dim lngEnd As Long
    lngStart = rng.Start
    lngEnd = rng.End

    .Find.ClearFormatting
    If strAr(intcFindStyle, iAr) <> "" Then
    .Find.Style = doc.Styles(strAr(intcFindStyle, iAr))
    Else
    End If
    If strAr(intcReplaceStyle, iAr) <> "" Then
    If boolStyleInDocument(strAr(intcReplaceStyle, iAr), doc) Then
    Else
    Dim lngType As Long
    lngType = wdStyleTypeParagraph
    If strAr(intcFindStyle, iAr) <> "" Then
    If boolStyleInDocument(strAr(intcFindStyle, iAr), doc) Then
    lngType = doc.Styles(strAr(intcFindStyle, iAr)).Type
    Else
    End If
    Else
    End If
    Call BuildStyle(strAr(intcReplaceStyle, iAr), doc, lngType)
    End If
    .Find.Replacement.Style = doc.Styles(strAr(intcReplaceStyle, iAr))
    Else
    End If

    With .Find
    .Text = strAr(intcFindText, iAr)
    .Replacement.Text = strAr(intcReplaceText, iAr)
    .Forward = True
    .Wrap = wdFindStop
    If strAr(intcFindStyle, iAr) <> "" Or strAr(intcReplaceStyle, iAr) <> "" Then
    .Format = True
    Else
    End If
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    While .Find.Execute(Replace:=wdReplaceOne)
    blnRuleText = True
    rng.Select
    rng.Start = rng.Start + 1
    rng.End = lngEnd
    Wend
    End With
    'Sub TESTblnRuleText()
    'Dim strAr(8, 0) As String
    ' strAr(0, 0) = ""
    ' strAr(1, 0) = ""
    ' strAr(2, 0) = ""
    ' strAr(3, 0) = "alpeph"
    ' strAr(4, 0) = ""
    ' strAr(5, 0) = "0"
    ' strAr(6, 0) = ""
    ' strAr(7, 0) = ""
    ' strAr(8, 0) = ""
    ' MsgBox blnRuleText(activedocument, strAr, 0)
    'End Sub
    End Function
    </pre>


  5. #5
    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: obtain the Range of a field? (Word97SR2/vba)

    Some of these fields are not like the others. I was trying to read a TA (Table of Authorities mark field) and .Result was not available, but .Code was. .Code also returns a Range object. Perhaps {Private} works like {TA}? They are both hidden. Hmmm... [ball back in your court]

Posting Permissions

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