Results 1 to 6 of 6

Thread: Word field

  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    860
    Thanks
    0
    Thanked 10 Times in 9 Posts
    Is it possible for VBA to determine if the insertion point is in a Word field? (Specifically, I'm looking for a REF field, but I know how to test for that once I get the field.)

    I want to get the value of Code.Text of the current field, providing the cursor is in a field.

    I have success with:

    Set rng = Selection.range
    rng.expand (wdWord)
    sCode = rng.Fields(1).Code.Text

    This fails, however, if field results are showing, and if the results are more than one word.

    Thanks.

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #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
    Maybe I missed a shortcut, but this actually seems a bit difficult when you start from the selection. It might be easier to cycle through the Fields collection and check whether the selection is in a field. But I didn't think of that before writing this...

    Code:
    Sub GetFieldCodeText()
    ' Make sure there is at least one field in this document
    If ActiveDocument.Fields.Count = 0 Then Exit Sub
    With Selection
        ' Store selection
        Dim strStartEnd As String
        strStartEnd = Selection.Start & "|" & Selection.End
        ' Expand selection to encompass the field
        Do
            If .Fields.Count > 0 Then Exit Do
            If .Start > 1 Then .MoveStart wdWord, -1
            If .End < ActiveDocument.Content.End - 1 Then .MoveEnd wdWord, 1
            If .Start <= 1 And .End >= ActiveDocument.Content.End - 1 Then Exit Do
        Loop
        ' Now do whatever with the field
        MsgBox .Fields(1).Code.Text
        ' Restore selection
        .Collapse wdCollapseStart
        .Start = Split(strStartEnd, "|")(0)
        .End = Split(strStartEnd, "|")(1)
    End With
    End Sub

  4. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    860
    Thanks
    0
    Thanked 10 Times in 9 Posts
    Thank you.

    I was expanding the range to wdWord, and that got only one word of the field if there were several. MoveStart and MoveEnd works well.

  5. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    I think this might be better:

    Code:
    Sub GetFieldCodeText()
    With Selection
        ' Check for fields in the selection
        If .Fields.Count > 0 Then
            MsgBox .Fields(1).Code.Text
        Else
            ' Check fields collection for a field that contains the selection
            Dim fld As Word.Field
            For Each fld In ActiveDocument.Fields
                ' Compare Selection.Start position to field Start and End
                If .Start >= fld.Result.Start And .Start <= fld.Result.End Then
                    MsgBox fld.Code.Text
                    Exit For
                End If
            Next
            If Not fld Is Nothing Then Set fld = Nothing
        End If
    End With
    End Sub

  6. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    860
    Thanks
    0
    Thanked 10 Times in 9 Posts
    The documents in question could have hundreds of field codes. Instead of looping through ActiveDocument.Fields, I can expand Selection.Range to the current paragraph, and then loop through each field in that much smaller range.

    Thank you for your help.

  7. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,899
    Thanks
    0
    Thanked 188 Times in 172 Posts
    Hi Richard,

    You might want to take a look at the macros posted by Jefferson and Andrew77 at:
    http://lounge.windowssecrets.com/ind...&st=0&p=320421
    both posts include macros to convert all fields in the selected range to textual representations - including nested fields. From memory, the results are copied to the clipboard, from where you can simply paste them wherever you want.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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