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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » obtain the Range of a field? (Word97SR2/vba)

    Author
    Topic
    #367677

    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 (“Ranges good; Selection bad“) 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.

    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
    
    
    Viewing 0 reply threads
    Author
    Replies
    • #574153

      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

      • #574963

        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”.

            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
        

        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.”

        • #575033

          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]

      • #574964

        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.

        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
        
    Viewing 0 reply threads
    Reply To: obtain the Range of a field? (Word97SR2/vba)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: