Re: Need selection to include a field

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Peter Hewett (nospam_at_xtra.co.nz)
Date: 06/12/04


Date: Sat, 12 Jun 2004 21:05:35 +1200

Hi David Thielen

Try the following code it will tell you if the selection is within, contains or is
immediately adjacent (cause the field shading to show) to a field:

Public Function SelectionContainsAField() As Boolean
    Dim rngAdjacent As Word.Range
    Dim rngParagraph As Word.Range
    Dim fldItem As Word.Field

    ' If the insertion point is in a field the field count
    ' is zero. This code allows for that problem.

    ' No more to do if the selected range contains a field
    If Selection.Fields.Count > 0 Then
        SelectionContainsAField = True
        Exit Function
    End If

    ' Copy the current selection into range objects
    ' so that we don't change the selection
    Set rngParagraph = Selection.Range
    Set rngAdjacent = Selection.Range

    ' Extend range to include current paragraph, if Selection is
    ' in a field the range is expanded to include that field
    rngParagraph.Expand wdParagraph

    For Each fldItem In rngParagraph.Fields

        ' If the selection is an insertion point adjacent to
        ' the field, then that also constitutes a match.
        If Selection.Type = wdSelectionIP Then
            If rngAdjacent.Start = fldItem.Code.Start - 1 Then
                SelectionContainsAField = True
                Exit For
            End If
        End If

        ' Make sure IP is really in a field and it's not just
        ' the expanded paragraph that picked up a field
        If Selection.InRange(fldItem.Result) Then
            SelectionContainsAField = True
            Exit For
        End If
    Next
End Function

HTH + Cheers - Peter
 

David Thielen <david@windward.net>, said:

>Hi;
>
>When the caret is on a field, the selection is for the text char just
>before the field. How can I find out if a selection is on a field?
>
>thanks - dave



Relevant Pages

  • Re: How to replace a string of characters with the count of the ch
    ... Dim AlphabetCountString As String ... I prefer to work with Range objects instead of the Selection whenever ... .MatchWildcards = False ...
    (microsoft.public.word.vba.general)
  • Ws Selection Change Event Code, Copy a Cell problem
    ... The sub below is doing what I want, re: selection. ... Dim RngType As String ... If ChgRow> PaEndRow Then ... Cells(ChgRow, SVrSubscrCol + COfs).Select ...
    (microsoft.public.excel.programming)
  • RE: Emailing With Attachments
    ... Presenting the file list in a combo box for selection ... Dim fso As New FileSystemObject ... 'Try to extract JobNum in the File name ... 'Set objOutlookRecip = .Recipients.Add ...
    (microsoft.public.access.modulesdaovba)
  • Re: Moving to next field with F11 in protected document
    ... macro is invoked with a keyboard hotkey and looks at the characters to ... a file with the name of the selection and inserts one space and then ... Dim rgeField As Range ... Dim lngStart As Long ...
    (microsoft.public.word.vba.beginners)
  • Re: Moving to next field with F11 in protected document
    ... a file with the name of the selection and inserts one space and then ... insertion point would move to the location following "postmarked on" ... Dim rgeField As Range ... Dim lngStart As Long ...
    (microsoft.public.word.vba.beginners)