![](/img/trans.png)
[英]How to get Selection.Text to read displaytext of Macro field code
[英]Using Cross Reference Field Code to move selection to Target of Field Code
OP更新:
感谢KazJaw的代码,它促使我改变了试图解决该问题的方法。 这是我当前的代码:
Sub Method3()
Dim intFieldCount As Integer
Dim i As Integer
Dim vSt1 As String
intFieldCount = ActiveDocument.Fields.Count
For i = 1 To intFieldCount
ActiveDocument.Fields(i).Select 'selects the first field in the doc
Selection.Expand
vSt1 = Selection.Fields(1).Code
'MsgBox vSt1
vSt1 = Split(vSt1, " ")(2) 'Find out what the (2) does
MsgBox vSt1
ActiveDocument.Bookmarks(vSt1).Select 'Selects the current crossreference in the ref list
Next i
End Sub
好的,这样代码现在可以找到文档中的第一个字段,读取其字段代码,然后跳转到文档中的位置以模仿CTRL + Click。
但是,它对所有类型的字段都执行此操作,例如书签,尾注,图形,表格等。我只想查找参考字段。 我以为我可以从域代码中推断出这一点,但事实证明,数字和书签使用的域代码布局相同。
有没有一种有效的方法可以使VBA区分两者? 我当时以为文档中的参考字段写为(参考1),我可以找到该字段,然后对左边的单词进行字符串比较以查看其是否显示“参考”。
我当时正在考虑使用MoveLeft方法来做到这一点
Selection.MoveLeft
但是我无法解决如何从当前选择中向左移动一个单词并选择该单词来做strcomp
或者也许我可以检查字段类型? 与...
If Selection.Type = wdFieldRef Then
Do Something
End If
但是我不确定我应该寻找哪个“类型”。
任何建议表示赞赏
所有REF字段都使用“引用”书签。 Word在为REF字段提供引用的所有对象上设置书签:数字,标题等。无法从字段的内容中区分另一端的内容。 您需要“检查”该目标,而无需实际选择即可完成。 例如,您可以检查前六个字母是否为“ Figure”。
您拥有的代码效率低下-无需使用Selection对象来获取域代码。 以下是更有效的:
Sub Method3()
Dim fld As Word.Field
Dim rng as Word.Range
Dim vSt1 As String
ForEach fld in ActiveDocument.Fields
vSt1 = fld.Code
'MsgBox vSt1
vSt1 = Split(vSt1, " ")(2) 'Find out what the (2) does
MsgBox vSt1
Set rng = ActiveDocument.Bookmarks(vSt1).Range
If Left(rng.Text, 6) <> "Figure" Then
rng.Select
End If
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.