如果是连续的,我需要删除所有新行,我知道 excel 中的宏但不知道 word 中的宏,我如何读取整行的值?
我用这个阅读了文档中的所有行:
For i = 1 To 10
Selection.MoveDown Unit:=wdLine, Count:=1
Next i
是否有其他方式来阅读每一行或如何知道一个单词的总行数以将其放入 for 中?
谢谢
如果是连续的,我需要删除所有新行
每个空行实际上是一个段落,所以:
Sub RemoveBlankParas()
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If Len(para.Range.Text) = 1 Then
'only the paragraph mark, so..
para.Range.Delete
End If
Next para
End Sub
但是,如果只有两个连续的空白段落,那么使用ReplaceAll
起来会更容易、更快捷。这是一个可以整理的录制宏:
Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Word 的查找/替换功能使用原始形式的正则表达式,因此以下减少了两个或多个连续的段落标记。注意MatchWildcards = True
使用正则表达式:
Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(^13)\1@"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub