Word VBA宏以在所选范围内的列表项末尾添加句号。

huangapple go评论87阅读模式
英文:

Word VBA macro to add a period at the end of list items within a selected range

问题

所以我突出显示一个带编号的列表或符号列表,然后运行宏。它应该检查编号列表的每个项目是否以句号“.”结尾。如果不以句号结尾,则忽略。

Sub AddPeriodonList()
    Dim para As Paragraph
    Dim lastChar As String
    Dim selectedRange As Range

    Set selectedRange = Selection.Range

    For Each para In selectedRange.Paragraphs
        If para.Range.ListFormat.listType <> wdListNoNumbering Then
            ' 获取列表项的最后一个可见字符(不包括段落标记)。
            lastChar = Right(para.Range.Text, 1)
            ' 从最后一个字符中去除段落标记
            If lastChar = vbCr Then
                lastChar = Right(para.Range.Text, 2)
            End If
            Debug.Print lastChar '(用于调试的立即窗口打印)
            ' 检查最后一个字符是否不是句号“。”
            If lastChar <> "." Then
                ' 在同一行的列表项末尾添加句号。
                para.Range.Characters(para.Range.Characters.Count - 2).InsertAfter "."
            End If
        End If
    Next para
End Sub

所以问题在于 lastChar 实际上返回了正确的字符,即编号列表中每个项目的最后一个字符,所以我现在正在尝试检查它是否为句号,但似乎不起作用,因为每次运行宏时都会向列表中的每个项目添加句号。

英文:

So i highlight a numbered list or bullet point, then i run the macro. It should check each item of the numbered list if it ends with period "." . If it doesn't end with a period, ignore

Sub AddPeriodonList()
    Dim para As Paragraph
    Dim lastChar As String
    Dim selectedRange As Range

    Set selectedRange = Selection.Range

    For Each para In selectedRange.Paragraphs
        If para.Range.ListFormat.listType &lt;&gt; wdListNoNumbering Then
            &#39; Get the last visible character of the list item (excluding paragraph marker).
            lastChar = Right(para.Range.Text, 1)
            &#39; Remove the paragraph marker from the last character
            If lastChar = vbCr Then
                lastChar = Right(para.Range.Text, 2)
            End If
            Debug.Print lastChar &#39; (print on immediate window for debugging)
            &#39; Checking if the last character is not a period &quot;.&quot;
            If lastChar &lt;&gt; &quot;.&quot; Then
                &#39; Add a period at the end of the list item on the same line.
                para.Range.Characters(para.Range.Characters.count - 2).InsertAfter &quot;.&quot;
            End If
        End If
    Next para
End Sub

So the problem is lastChar actually returns the correct character, which is the last character of each item on the numbered list, So i am now trying to check that if it is period or not but seems not to work, as the macro adds period to every item on the list every time its being run.

答案1

得分: 0

Right(para.Range.Text, 2) 返回字符串的最后两个字符。请使用 Mid 来获取字符。

Sub AddPeriodonList()
    Dim para As Paragraph
    Dim lastChar As String, sTxt As String
    Dim selectedRange As Range

    Set selectedRange = Selection.Range

    For Each para In selectedRange.Paragraphs
        If para.Range.ListFormat.ListType &lt;&gt; wdListNoNumbering Then
            sTxt = para.Range.Text
            ' 获取列表项的最后一个可见字符(不包括段落标记)。
            lastChar = Right(sTxt, 1)
            ' 从最后一个字符中移除段落标记
            If lastChar = vbCr Then
                lastChar = Mid(sTxt, Len(sTxt) - 1, 1)
            End If
            Debug.Print lastChar '(用于调试的即时窗口打印)
            ' 检查最后一个字符是否不是句号“。”。
            If lastChar &lt;&gt; "。" Then
                ' 在同一行的列表项末尾添加句号。
                para.Range.Characters(para.Range.Characters.Count - 1).InsertAfter "。"
            End If
        End If
    Next para
End Sub
英文:

Right(para.Range.Text, 2) returns the last two characters of a string. Please use Mid to get a characters.

Sub AddPeriodonList()
    Dim para As Paragraph
    Dim lastChar As String, sTxt As String
    Dim selectedRange As Range

    Set selectedRange = Selection.Range

    For Each para In selectedRange.Paragraphs
        If para.Range.ListFormat.ListType &lt;&gt; wdListNoNumbering Then
            sTxt = para.Range.Text
            &#39; Get the last visible character of the list item (excluding paragraph marker).
            lastChar = Right(sTxt, 1)
            &#39; Remove the paragraph marker from the last character
            If lastChar = vbCr Then
                lastChar = Mid(sTxt, Len(sTxt) - 1, 1)
            End If
            Debug.Print lastChar &#39; (print on immediate window for debugging)
            &#39; Checking if the last character is not a period &quot;.&quot;
            If lastChar &lt;&gt; &quot;.&quot; Then
                &#39; Add a period at the end of the list item on the same line.
                para.Range.Characters(para.Range.Characters.Count - 1).InsertAfter &quot;.&quot;
            End If
        End If
    Next para
End Sub

huangapple
  • 本文由 发表于 2023年8月5日 04:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定