VBA实现检查和删除Word中的空白页
来源:互联网 发布:美萍进销存软件使用 编辑:程序博客网 时间:2024/04/30 01:51
word中的空白页,可能由于回车产生,还有可能由插入的分页符产生,以下代码通过读取每一页的数据并判断,实现对Word中
空白页的检查,并可实现自动删除!
在word中,插入一个模块,复制如下代码
Option Explicit
Sub GetBlankPage()
Dim IsDelete As Boolean
Dim PageCount As Long
Dim rRange As Range
Dim iInt As Integer, DelCount As Integer
Dim tmpstr As String
IsDelete = True
PageCount = ThisDocument.BuiltInDocumentProperties(wdPropertyPages)
For iInt = 1 To PageCount
'超过PageCount退出
If iInt > PageCount Then Exit For
'取每一页的内容
If iInt = PageCount Then
Set rRange = ThisDocument.Range( _
Start:=ThisDocument.GoTo(wdGoToPage, wdGoToAbsolute, iInt).Start)
Else
Set rRange = ThisDocument.Range( _
Start:=ThisDocument.GoTo(wdGoToPage, wdGoToAbsolute, iInt).Start, _
End:=ThisDocument.GoTo(wdGoToPage, wdGoToAbsolute, iInt + 1).Start _
)
End If
If Replace(rRange.Text, Chr(13), "") = "" Or Replace(rRange.Text, Chr(13), "") = Chr(12) Then
tmpstr = tmpstr & "第 " & iInt & " 页是空页" & vbCrLf
'删除?
If IsDelete Then
DelCount = DelCount + 1
'删除空白页
rRange.Text = Replace(rRange.Text, Chr(13), "")
rRange.Text = ""
'重算页数
PageCount = ThisDocument.BuiltInDocumentProperties(wdPropertyPages)
If iInt <> PageCount Then
'页删除后,页码变化,重新检查当前页
iInt = iInt - 1
Else
'最后一个空页
Set rRange = ThisDocument.Range( _
Start:=ThisDocument.GoTo(wdGoToPage, wdGoToAbsolute, PageCount - 1).Start, _
End:=ThisDocument.GoTo(wdGoToPage, wdGoToAbsolute, PageCount + 1).Start _
)
'如果是分页符,删除上一页中的换页符
If InStr(1, rRange.Text, Chr(12)) > 0 Then
rRange.Characters(InStr(1, rRange.Text, Chr(12))) = ""
Else
'没有分页符,通过选中后删除,最好不这样做,如果判断错误,有误删除的风险
Set rRange = ThisDocument.Range( _
Start:=ThisDocument.GoTo(wdGoToPage, wdGoToAbsolute, iInt).Start)
rRange.Select
Selection.Delete
End If
Exit For
End If
End If
End If
Next
If 1 = 1 Or Not IsDelete Then
If tmpstr = "" Then
MsgBox "没有空页", vbInformation + vbOKOnly
Else
MsgBox tmpstr, vbInformation + vbOKOnly
End If
Else
If DelCount > 0 Then MsgBox "删除空页 " & DelCount, vbInformation + vbOKOnly
End If
End Sub
- VBA实现检查和删除Word中的空白页
- 如何删除word中的空白页
- word中删除空白页
- word怎么删除空白页
- word空白页删除
- mac 删除Microsoft office word中的空白页的方法
- word删除空白页的方法
- Word删除空白页方法汇总
- word中删除多余的空白页
- [转贴]Word删除空白页方法汇总
- 如何删除word文档最后一页空白页
- Word技巧_删除多余的空白页
- 保留分页符 删除WORD空白页
- word文档中如何删除空白页
- word文档中如何删除空白页
- word中如何删除一页空白页
- 写论文中word页脚和脚注的问题和删除空白页
- word2007 同一个word文档设置不同页眉和删除设置页眉后出现的空白页
- 什么是RIA
- CSDN VC编程经验总结(转)
- [转,校园招聘]google2009笔试题
- 真累
- 管理十大经典理论终生受用
- VBA实现检查和删除Word中的空白页
- 剩饭--华丽的转身
- 有必要吗?
- 投票系统防刷票
- 从词汇的地域变异看词汇语意的描述
- 学习SQL应知道的动态SQL语句基本语法
- 拒绝浮躁
- actionScript 3.0 图片裁剪及旋转
- 《疯狂的程序员》精句摘录(一)