|
看似简单的代码写起来还是蛮复杂的,容错还是没有写完。。。- Sub 分割文档()
- Application.ScreenUpdating = False
- Dim PageCount&, SavePath$, aPage&, specialStr$
- Dim oStart&, oEnd&, aDoc As Document
- Set aDoc = ThisDocument
- PageCount = aDoc.ComputeStatistics(wdStatisticPages) '获取总页数
- SavePath = aDoc.Path & "\拆分" '保存目录
- If Dir(SavePath, vbDirectory) = "" Then MkDir SavePath '如果目录不存在就建立目录
- For aPage = 1 To PageCount '循环处理所有页
- oStart = aDoc.GoTo(wdGoToPage, wdGoToAbsolute, aPage).Start '本页页首坐标
- If aPage < PageCount Then
- oEnd = aDoc.GoTo(wdGoToPage, wdGoToAbsolute, aPage + 1).Start '次页页首坐标
- Else
- oEnd = aDoc.Content.End
- End If
- specailStr = Chr(11) & Chr(12) & Chr(13) & Chr(14) '页尾特殊字符列表
- If InStr(1, specialStr, aDoc.Range(oEnd - 1, oEnd).Text, vbBinaryCompare) <> 0 Then '页尾是特殊字符时
- oEnd = oEnd - 1 '坐标向前移动一位
- End If
- aDoc.Range(oStart, oEnd).Copy '复制页内容
- With Documents.Add '新建文档,粘贴,另存
- .Range.Paste
- .SaveAs SavePath & "\第" & CStr(aPage) & "页"
- .Close
- End With
- Next
- Application.ScreenUpdating = True
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|