在我的 Word 文档中,我有两个“部分”。这是屏幕截图:
不幸的是,我无法使用此代码访问右侧的“部分”:
Dim rng As Range
Dim rngText As Range
Set rng = ActiveDocument.Range
Set rngText = ActiveDocument.Range(0, 0)
rngText.Collapse wdCollapseStart
With rng.Find
.Format = False
右侧的这个“部分”是范围吗?如何使用上面的代码访问它?
编辑:我现在更接近了。这是一个文本字段。
我现在可以访问它了。下面的代码显示了消息框中文本字段的文本。但是,它找不到文本。我不得不删除“rng”行,因为我不知道该怎么处理它。
有人可以帮忙吗?
谢谢你!
For Each s In ActiveDocument.Shapes
With s.TextFrame
If .HasText Then MsgBox .TextRange.Text
Dim bDelete As Boolean
bDelete = False
'Dim rng As Range
Dim rngText As Range
'Set rng = s.Range
Set rngText = .TextRange
rngText.Collapse wdCollapseStart
' Search-Loop
With rngText.Find
.Format = False
.Text = "{"
'Search for start tag
.Execute
Do While .Found = True
'Store found position with start tag
'rngText.SetRange rng.Start, rng.End
'Reduce search are
'rng.SetRange rng.End, ActiveDocument.Range.End
'Search for end tag
.Execute findText:="}", Forward:=True
'If none found, exit
If .Found = False Then Exit Sub
'Expand found position until tag end
rngText.SetRange rngText.Start, rng.End
If bDelete = False Then
'make the found text bold and give it a certain color
rngText.Select
rngText.Font.Bold = True
rngText.Font.Color = -553582593
rngText.Text = VBA.Mid(rngText.Text, 2, rngText.End - rngText.Start - 2)
Else
'Delete it
rngText.Delete
End If
'Reduce search area until end position
rng.Collapse wdCollapseEnd
'Search for start tag
.Execute findText:="{", Forward:=True
Loop
'rng.Collapse wdCollapseEnd
End With
End With
Next