为什么这个单词宏不使斜体成为包含括号的所有行的第一部分?

问题描述 投票:0回答:1

我有一个 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
vba ms-word
1个回答
0
投票
  • Set myRange = newDoc.Paragraphs(i).Range
    myRange
    是段落范围。
  • 更改
    MoveStart
    MoveEnd
  • 的字符数

微软文档:

Range.MoveStart 方法(Word)

Range.MoveEnd 方法(Word)

               If ePos > bPos Then
                  myRange.MoveStart wdCharacter, bPos
                  myRange.MoveEnd wdCharacter, ePos - lineLength - 1
                  myRange.Font.Italic = wdToggle
               End If
© www.soinside.com 2019 - 2024. All rights reserved.