我有一个 Word 文档,其中包含我扫描的书籍的索引。该索引的某些行本身引用了书籍,并且这些行带有括号。示例行可能是:
罗马帝国的兴衰(诺顿及其同事)
所以我搜索其中包含“(”的行,然后尝试将该行中所有先前的单词设为斜体。它不起作用 - 太多字符被设为斜体。这是我的尝试:
Sub SelectPreBracketLetters(ByRef newDoc As Document)
Dim i As Integer
Dim lineText As String
Dim lineLength As Integer
Dim selectionStart As Integer
Dim selectionEnd As Integer
Dim bPos As Integer
Dim ePos As Integer
Dim myRange As Range
For i = 1 To newDoc.Paragraphs.Count
Set myRange = newDoc.Paragraphs(i).Range
lineText = myRange.text
lineLength = Len(lineText)
If lineLength > 2 Then
bPos = InStr(lineText, "(")
If bPos > 2 Then
ePos = InStr(lineText, ")")
If ePos > bPos Then
' MsgBox ("bpos is " & bPos & " and line is " & lineText)
myRange.MoveStart wdCharacter, 1
myRange.MoveEnd wdCharacter, bPos - 1
myRange.Font.Italic = wdToggle
End If
End If
End If
Next i
End Sub
Set myRange = newDoc.Paragraphs(i).Range
myRange
是段落范围。MoveStart
和 MoveEnd
微软文档:
If ePos > bPos Then
myRange.MoveStart wdCharacter, bPos
myRange.MoveEnd wdCharacter, ePos - lineLength - 1
myRange.Font.Italic = wdToggle
End If