光标在哪个字段中? (ms word,vba)

问题描述 投票:3回答:4

在VBA Word宏中,我想为包含光标的字段获取Field对象。

明显的尝试失败了:

Private Sub Try1()
    MsgBox Selection.Fields.Count
End Sub

数组是空的。然后我尝试了:

Private Sub Try2()
    Dim oRange As Range
    Set oRange = Selection.GoTo(What:=wdGoToField)
    MsgBox oRange
End Sub

光标不移动,消息为空。

我可以迭代ActiveDocument.Fields,比较范围并找到包含的fiels。但可能有一个简单的直接方式?

ms-word word-vba
4个回答
1
投票

我目前的生产代码迭代超过Document.Fields

Sub Test()
    Dim oField As Field
    Set oField = FindWrappingField(Selection.Range)
    If oField Is Nothing Then
        MsgBox "not found"
    Else
        MsgBox oField
    End If
End Sub

Private Function FindWrappingField(vRange As Range)
    Dim oField As Field
    Dim nRefPos As Long
    ' If selection starts inside a field, it also finishes inside.
    nRefPos = vRange.Start
    ' 1) Are the fields sorted? I don't know.
    '    Therefore, no breaking the loop if a field is too far.
    ' 2) "Code" goes before "Result", but is it forever?
    For Each oField In vRange.Document.Fields
        If ((oField.Result.Start <= nRefPos) Or (oField.Code.Start <= nRefPos)) And _
            ((nRefPos <= oField.Result.End) Or (nRefPos <= oField.Code.End)) Then
                Set FindWrappingField = oField
                Exit Function
        End If
    Next oField
    Set FindWrappingField = Nothing
End Function

0
投票

我有同样的问题,我解决了以下代码:

Sub Test()
    NumberOfFields = Selection.Fields.Count
    While NumberOfFields = 0
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        NumberOfFields = Selection.Fields.Count
    Wend
End Sub

当然,我必须知道光标在一个字段中。显然,当您选择向右延伸的范围时,将在某个时刻选择该字段。范围的结束不计算(它不使用字段范围)


0
投票

我用这个代码

Sub GetFieldUnderCursor()
Dim NumberOfFields As Integer
Dim oFld As Field
Dim TextFeld As String
Dim Typ As Integer
Dim pos As Integer
Dim NameOfField As String
'update field. Cursor moves after the field
Selection.Fields.Update
'select the field
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
'check if there is a field
NumberOfFields = Selection.Fields.Count
If NumberOfFields = 0 Then
    MsgBox "No field under cursor"
    Exit Sub
End If
Set oFld = Selection.Fields(1)
TextFeld = Trim(oFld.Code.Text)
Typ = oFld.Type '85 is DOCPROPERTY, 64 is DOCVARIABLE
If Typ = 85 Or Typ = 64 Then
    pos = InStr(15, TextFeld, " ")
    If pos > 0 Then
        NameOfField = Trim(Mid(TextFeld, 12, pos - 11))
        MsgBox NameOfField
    End If
End If

结束子


0
投票

以下函数确定选择是跨越还是在字段内。

Function WithInField(Rng As Word.Range) As Boolean
' Based on code by Don Wells: http://www.eileenslounge.com/viewtopic.php?f=30&t=6622
' Approach  : This procedure is based on the observation that, irrespective of _
              a field's ShowCodes state, toggling the field's ShowCodes state _
              twice collapses the selection to the start of the field.
Dim lngPosStart As Long, lngPosEnd As Long, StrNot As String
WithInField = True
Rng.Select
lngPosStart = Selection.Start
lngPosEnd = Selection.End
With Selection
  .Fields.ToggleShowCodes
  .Fields.ToggleShowCodes
  ' Test whether the selection has moved; if not, it may already have been _
    at the start of a field, in which case, move right and test again.
  If .Start = lngPosStart Then
    .MoveRight
    .Fields.ToggleShowCodes
    .Fields.ToggleShowCodes
    If .Start = lngPosStart + 1 Then
      WithInField = False
    End If
  End If
End With
End Function

您可以使用以下代码的函数:

Sub TestWithInField()
Dim Rng As Word.Range, c As Word.Range, StrRslt As String
Set Rng = Selection.Range
For Each c In Rng.Characters
    StrRslt = StrRslt & c.Text & ",WithInField:" & WithInField(Rng:=c) & vbCr
Next
Rng.Select
MsgBox StrRslt
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.