Vba 如果句号和尾注引用(上标)之间没有空格,Sub无法找到长句
我有一个简单的循环(如下),寻找超过30个单词的句子。如果找到,它会在所选句子中添加注释框。它在测试中运行良好。然后我添加了一些测试尾注引文…但它没有找到长句 但是,只有在句点和引文上标之间没有空格时才会失败。如果我添加一个空格,它会找到它并完美地工作。问题是,根据我在工作中必须遵循的风格指南,句号和引用之间不存在空格 讨论句点后是否需要一个空格来描绘句子的结尾。我假设空格必须在句号之后,因为我的引文中有空格,比如1,2,3 问题 如何找到句点+上标(没有像这样的空格-->这是一个句子。1、2、3)的实例并添加空格?理想情况下,我希望这发生在下面的循环中,这样我可以在添加注释后删除空格Vba 如果句号和尾注引用(上标)之间没有空格,Sub无法找到长句,vba,ms-word,word,sentence,citations,Vba,Ms Word,Word,Sentence,Citations,我有一个简单的循环(如下),寻找超过30个单词的句子。如果找到,它会在所选句子中添加注释框。它在测试中运行良好。然后我添加了一些测试尾注引文…但它没有找到长句 但是,只有在句点和引文上标之间没有空格时才会失败。如果我添加一个空格,它会找到它并完美地工作。问题是,根据我在工作中必须遵循的风格指南,句号和引用之间不存在空格 讨论句点后是否需要一个空格来描绘句子的结尾。我假设空格必须在句号之后,因为我的引文中有空格,比如1,2,3 问题 如何找到句点+上标(没有像这样的空格-->这是一个句子。1、2、
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实践 你的问题很常见。然后找一些东西,而不是做替换,做一些其他与替换无关的事情。引用前添加和删除空格的sub实现了这种模式,非常值得研究 如果您不理解任何内容,那么在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.句子属性或.Word属性的代码都将产生不可靠的结果。这是因为。句子不知道什么是语法句子。单词不知道什么是语法单词。例如,考虑以下内容: 史密斯先生在约翰博士的杂货店花了1234.56美元,买了10.25公斤土豆、10公斤鳄梨和15.1公斤格林夫人的普莱森特澳洲坚果
对你和我来说,这算是一个26字的句子;对于VBA,它计为5个句子,总共包含45个单词。要获得准确的字数,请使用.computeTestatics(wdStatisticWords)。遗憾的是,句子中没有等效的.computeTestatics(wdStatisticSentences)。很抱歉声明-我是新手,因此感谢您的帮助。每天学习。