英文:
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 <> wdListNoNumbering Then
' Get the last visible character of the list item (excluding paragraph marker).
lastChar = Right(para.Range.Text, 1)
' Remove the paragraph marker from the last character
If lastChar = vbCr Then
lastChar = Right(para.Range.Text, 2)
End If
Debug.Print lastChar ' (print on immediate window for debugging)
' Checking if the last character is not a period "."
If lastChar <> "." Then
' Add a period at the end of the list item on the same line.
para.Range.Characters(para.Range.Characters.count - 2).InsertAfter "."
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 <> 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 <> "。" 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 <> wdListNoNumbering Then
sTxt = para.Range.Text
' Get the last visible character of the list item (excluding paragraph marker).
lastChar = Right(sTxt, 1)
' Remove the paragraph marker from the last character
If lastChar = vbCr Then
lastChar = Mid(sTxt, Len(sTxt) - 1, 1)
End If
Debug.Print lastChar ' (print on immediate window for debugging)
' Checking if the last character is not a period "."
If lastChar <> "." Then
' Add a period at the end of the list item on the same line.
para.Range.Characters(para.Range.Characters.Count - 1).InsertAfter "."
End If
End If
Next para
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论