我正在尝试遍历每一行或每段,并突出显示文本文档中特定范围的文本变量intSortPos是一个整数数组,用于保存起始位置和开始位置之后的位置数量,例如hightlight intSortPos(0)是起始位置,intSortPos(1)是从intSortPos(0)移动的空格量
当我运行此代码时,它只突出显示第一行,虽然我可以在调试器中看到它正在将范围设置为其他段落,尽管它不会像它应该那样更改它们。
Public Sub Highlight()
Dim para As Paragraph
Dim i As Integer
Dim j As Integer
Dim rng As Range
Dim rngGray As Range
Dim start As Integer
Dim move As Integer
start = 0
move = 1
For Each para In ActiveDocument.Paragraphs
'MsgBox (">" & para.Range.Text)
Set rng = para.Range
rng.SetRange start:=intSortPos(start), End:=intSortPos(start) + intSortPos(move)
rng.HighlightColorIndex = wdYellow
'start = start + 2
'move = move + 2
Next para
End Sub
答案 0 :(得分:1)
rng.SetRange start:=0, End:1
表示整个文档中的第一个字符,而不是段落中的第一个字符。
因此,在设置范围开始时,您必须使用rng.SetRange start:=rng.Start
。
我修改了你的代码,以突出显示每个段落中的前10个字符:
Option Explicit
Public Sub Highlight()
Dim para As Paragraph
Dim i As Integer
Dim j As Integer
Dim rng As Range
Dim rngGray As Range
Dim move As Integer
Dim startHighlight As Integer
Dim endHighlight As Integer
For Each para In ActiveDocument.Paragraphs
Set rng = para.Range
startHighlight = rng.start
endHighlight = rng.start + 10
rng.SetRange start:=startHighlight, End:=endHighlight
rng.HighlightColorIndex = wdYellow
Next para
End Sub
希望它有所帮助。
答案 1 :(得分:0)
以下是突出显示段落中特定区域的工作代码 我使用所谓的排序卡来决定亮点的位置。在运行宏之前,排序卡与数据一起粘贴到文档中。 这是我的排序卡的样子
排序字段=(30,4,CH,A,5,12,CH,A,17,13,CH,A,3,2,CH,A)
30是第一个起始位置,4是该位置的空格量。下一个高亮显示位置为5,我们突出显示的空格数为12.这只是排序卡的持续时间。
Public Sub SetPositions()
Dim myRange As Object
Dim i As Integer
Dim strSortCard As String
Dim Char As Variant
Dim Char2 As Variant
Dim blNumeric As Boolean
Dim blNumeric2 As Boolean
Dim intDoubleDigit As Integer
Set myRange = ActiveDocument.Range
selection.ClearFormatting
selection.MoveUp Unit:=wdScreen, count:=1
With ActiveDocument.Content.Find
.Text = "Sort Fields=("
.Forward = True
.Execute
If .Found = True Then .Parent.Bold = True
End With
'selection.MoveRight Unit:=wdCharacter, count:=1
'selection.SetRange Start:=5, End:=6
selection.Expand wdLine
strSortCard = selection.Text
strSortCard = RTrim(strSortCard)
ReDim intSortPos(16)
For i = 1 To Len(strSortCard)
Char = Mid(strSortCard, i, 1)
blNumeric = IsNumeric(Char)
'if is numeric
If (blNumeric) Then
blNumeric = False
'check the next char for a number
Char2 = Mid(strSortCard, i + 1, 1)
blNumeric2 = IsNumeric(Char2)
If (blNumeric2) Then
blNumeric2 = False
intDoubleDigit = CInt(Char) & CInt(Char2)
intSortPos(intCountPos) = CInt(intDoubleDigit)
'MsgBox (intSortPos(intCountPos))
i = i + 1
Else
intSortPos(intCountPos) = CInt(Char)
'MsgBox (intSortPos(intCountPos))
End If
intCountPos = intCountPos + 1
End If
Next
'For i = 0 To 7
'MsgBox (intSortPos(i))
'Next
Call Highlight
End Sub
Public Sub Highlight()
Dim para As Paragraph
Dim i As Integer
Dim j As Integer
Dim rng As Range
Dim rngGray As Range
Dim start As Integer
Dim move As Integer
Dim startHighlight As Integer
Dim endHighlight As Integer
Dim intParaStart As Integer
Dim blFirstline As Boolean
start = 0
move = 1
blFirstline = True
For Each para In ActiveDocument.Paragraphs
If blFirstline Then
para.Next
blFirstline = False
End If
'MsgBox (">" & para.Range.Text)
selection.Find.ClearFormatting
Set rng = para.Range
'rng.start = intSortPos(start)
startHighlight = rng.start + intSortPos(start + 0) - 1
endHighlight = startHighlight + intSortPos(move + 0)
rng.SetRange start:=startHighlight, End:=endHighlight
rng.HighlightColorIndex = wdYellow
Set rng = para.Range
startHighlight = rng.start + intSortPos(start + 2) - 1
endHighlight = startHighlight + intSortPos(move + 2)
rng.SetRange start:=startHighlight, End:=endHighlight
rng.HighlightColorIndex = wdBlue
Set rng = para.Range
startHighlight = rng.start + intSortPos(start + 4) - 1
endHighlight = startHighlight + intSortPos(move + 4)
rng.SetRange start:=startHighlight, End:=endHighlight
rng.HighlightColorIndex = wdRed
Set rng = para.Range
startHighlight = rng.start + intSortPos(start + 6) - 1
endHighlight = startHighlight + intSortPos(move + 6)
rng.SetRange start:=startHighlight, End:=endHighlight
rng.HighlightColorIndex = wdViolet
Next para