如果是连续的,我需要删除所有新行,我在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的"查找/替换"功能使用了正则表达式的基元形式,因此以下内容减少了两个或多个连续的段落标记。NB 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
单词正则表达式