找出句号和尾注引用(上标)之间没有空格的长句



我有一个循环,用于查找长度超过30个单词的句子。如果找到,它会在所选句子中添加一个注释框。它在测试中发挥了作用。然后我添加了一些测试尾注引用,但没有找到长句。

当句号和引文上标之间没有空格时,它将失败。根据我在工作中必须遵循的风格指南,句号和引文之间不应该有空格。

这个相关的Stack线程讨论了在句号之后需要一个空格来描绘句子的结尾。我假设空格必须直接在句号之后,因为我的引文中有空格,如1,2,3

如何找到句点+上标的实例(没有这样的空格-->这是一个句子。1,2,3(并添加空格?理想情况下,我希望这发生在下面的循环中,这样我就可以在添加评论后删除空格。

Sub Comment_on_Long_Sentences ()
Dim iWords as Integer
iWords = 0
For Each MySent in ActiveDocument.Sentences
If MySent.Words.Count > iWords Then
MySent.Select
'find and delete space
ActiveDocument.Comments.Add Range:= Selection.Range, Text:= "Long Sentence: " & iWords & " words"
'put the space back
End if
Next MySent
End Sub

试图访问以上标字符结尾的句子时,VBA中似乎存在问题。您的代码也有未声明变量的问题,所以我不知道它最初是如何为您工作的。

试试下面的VBA例程,它可以在我的环境中工作。还要注意,我发现段落中的第一句以及该句以上标字符结尾时需要特殊处理。

Sub Comment_on_Long_Sentences()
Dim doc As word.Document, rng As word.Range, para As word.Paragraph
Dim i As Long
Set doc = ActiveDocument
For Each para In doc.Paragraphs
Debug.Print para.Range.Sentences.Count
For i = 1 To para.Range.Sentences.Count
Set rng = para.Range.Sentences(i)
If i = 1 And rng.Characters.First.Font.Superscript = True Then
rng.MoveStart word.WdUnits.wdSentence, Count:=-1
End If
If rng.words.Count > 30 Then
doc.Comments.Add Range:=rng, Text:="Long Sentence: " & rng.words.Count & " words"
End If
Next
Next
End Sub

这里有一个替代解决方案。请注意选项在开始处显式显示。将其放在每个模块的顶部是VBA的良好实践。

你遇到的问题很常见。然后找一些东西,而不是做替换,做一些其他与替换无关的事情。引用前添加和删除空格的子实现了这种模式,非常值得研究。

如果您什么都不懂,那么在VBA IDE中,只需将光标放在相关关键字上,然后按F1。这将打开相关的MS帮助页面。

Option explicit
Sub Comment_on_Long_Sentences()
Dim iWords                          As Integer
Dim my_sentence                     As Variant
iWords = 30
AddSpaceBeforeCitations
For Each my_sentence In ActiveDocument.Sentences
If my_sentence.Words.Count > iWords Then
my_sentence.Comments.Add Range:=my_sentence, Text:="Long Sentence: " & iWords & " words"
End If
Next my_sentence
RemoveSpaceBeforeCitations
End Sub
Sub AddSpaceBeforeCitations()
With ActiveDocument.Content
With .Find
.ClearFormatting
.Format = True
.Text = ""
.Wrap = wdFindStop
.Font.Superscript = True
.Execute
End With
Do While .Find.Found
With .Previous(unit:=wdCharacter, Count:=1).characters
If .Last.Text = "." Then
.Last.Text = ". "
End If
End With
.Collapse direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=1
.Find.Execute
Loop
End With
End Sub
Sub RemoveSpaceBeforeCitations()
With ActiveDocument.Content
With .Find
.ClearFormatting
.Format = True
.Text = ""
.Wrap = wdFindStop
.Font.Superscript = True
.Execute
End With
Do While .Find.Found
With .Previous(unit:=wdCharacter, Count:=2).characters
If (.Last.Text = ".") Then
.Last.Next(unit:=wdCharacter, Count:=1).characters.Last.Text = vbNullString
End If
End With
.Collapse direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=1
.Find.Execute
Loop
End With
End Sub

无论采用何种方法,任何依赖VBA的.Spension属性或.Word属性的代码都会产生不可靠的结果。这是因为.句子不知道什么是语法句子。.单词不知道语法单词是什么。例如,考虑以下内容:

先生。史密斯在约翰博士杂货店花了1234.56美元买了10.25公斤土豆、10公斤鳄梨和15.1公斤格林夫人的Mt.Pleasant澳洲坚果

对你我来说,这算是一个26个单词的句子;VBA共有5个句子,共包含45个单词。要获得准确的字数,请使用.ComputerStatistics(wdStatisticWords(。遗憾的是,句子并没有.ComputerStatistics(wdStatisticPences(的等价物。

最新更新