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

光标在哪个字段中?(微软word,vba),vba,ms-word,Vba,Ms Word,在VBA Word宏中,我希望为包含光标的字段获取一个字段-对象 显而易见的尝试失败了: 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 光标不移动,消息为

在VBA Word宏中,我希望为包含光标的字段获取一个
字段
-对象

显而易见的尝试失败了:

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
,比较范围并找到包含的字段。但可能有一种简单直接的方法?

我当前的生产代码,在
文档上迭代。字段

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
子测试()
作为场的场的暗度
字段集=FindRappingField(Selection.Range)
如果这片土地什么都不是
MsgBox“未找到”
其他的
MSGBoxofield
如果结束
端接头
专用函数FindRappingField(vRange作为范围)
作为场的场的暗度
将NREFPO变暗为长
'如果选择从字段内部开始,它也在字段内部结束。
nRefPos=vRange.Start
“1)字段是否已排序?我不知道。
因此,如果一个字段太远,就不能中断循环。
“代码”在“结果”之前,但它是永远的吗?
对于vRange.Document.Fields中的每个字段

如果((oField.Result.Start我有同样的问题,我用下面的代码解决了:

Sub Test()
    NumberOfFields = Selection.Fields.Count
    While NumberOfFields = 0
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        NumberOfFields = Selection.Fields.Count
    Wend
End Sub
当然,我必须知道光标在字段中。 显然,当您选择一个向右延伸的范围时,某个时刻该字段将被选中。该范围的结尾不算(它不算字段范围)

我使用此代码

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

End Sub

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

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

您的文档中有什么类型的
字段?@KazimierzJawor类型是
DOCPROPERTY
。我检查了一些选项,但没有任何效果,例如,我检查了在某些情况下此代码
选择。展开wdWord
选择整个字段,但这不是一条规则。似乎您需要使用循环来确保您处于fi中eld.事情比预期的要复杂。如果字段位于“不在文档中”,则代码找不到该字段,因此在页眉、页脚、文本框或类似内容中。对于这种情况,我现在使用了更多循环。外部循环位于
vRange.Document.StoryRanges
上方,下一个循环位于
上方。下一个循环位于
上方,最嵌套的循环位于
上方。字段
上方。此外,我还将当前故事与参考故事进行了比较。这很好。有wever a small bug.您需要添加:
Selection.collapse
介于
Rng.Select
lngPosStart=Selection.Start
。如果不包括此项,如果在选择之前有一个字段,该过程将返回true。我还建议将光标重新定位在原来的位置。这可以通过添加
Rng。选择
作为带内场的
子菜单的最后一行。