|
楼主 |
发表于 2009-11-20 15:10
|
显示全部楼层
解
Sub SaveAsPages()
Dim NumberPages As Integer, SAP As Byte, i As Integer
Dim lngStart As Long, lngEnd As Long, myRange As Range
Dim NewDoc As Document, FilName As String
On Error GoTo Errhandle '错误处理
SAP = VBA.InputBox("请输入每个文件的分页数", "ExcelHome", Default:=1)
With ThisDocument
'取得本文档的总页数
NumberPages = .Content.Information(wdNumberOfPagesInDocument)
'MsgBox NumberPages
'如果分页值大于总页数或者为0的整数则进入错误处理行
If SAP > NumberPages Or SAP = 0 Then GoTo Errhandle
Application.ScreenUpdating = False '关闭屏幕更新
'将分节符替换为分页符
.Content.Find.Execute findtext:="^b", Replacewith:="^m", Replace:=wdReplaceAll
For i = 1 To NumberPages Step SAP '进行一个循环
'取得第一个分页的起始位置
lngStart = .GoTo(wdGoToPage, wdGoToNext, , i).Start
'取得第一个分页的最后位置(是下一页的开始位置)
lngEnd = VBA.IIf(i + SAP > NumberPages, .Content.End, .GoTo(wdGoToPage, wdGoToNext, , i + SAP).Start)
'定义一个RANGE对象
Set myRange = .Range(lngStart, lngEnd)
'
'取得文件名,为该RANGE区域中的第一个段落文本(去除段落标记)
FilName = CInt(i)
Set NewDoc = Documents.Add '新建一个空白文档
myRange.Copy '复制
With NewDoc
.Range(0, 0).Paste '起点处粘贴
.Paragraphs.Last.Range.Delete '删除最后一个段落标记(看情况)
' Debug.Print FilName
With ActiveDocument.PageSetup
.TopMargin = CentimetersToPoints(2)
.BottomMargin = CentimetersToPoints(0.6)
.LeftMargin = CentimetersToPoints(3.2)
.RightMargin = CentimetersToPoints(3.2)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(0)
.FooterDistance = CentimetersToPoints(0)
.PageWidth = CentimetersToPoints(39)
.PageHeight = CentimetersToPoints(28.5)
End With
.SaveAs FilName '另存为
.Close '关闭该文档
End With
Next
End With
Application.ScreenUpdating = True '恢复屏幕更新
Exit Sub
Errhandle:
MsgBox "无效的分页数,请输入正确的数值!", vbExclamation, "ExcelHome"
Application.ScreenUpdating = True
End Sub
曾经论坛里面有个守柔兄提供的程序,,,曾尝试及部分修改后,,对偶的文档可行。。。主要是文档命名还没解决,,因为原先守柔兄的程序行头是一小段文字可以提取当文档名,,而偶的不行,,所以命名只好用1.3.5.7来命名。 |
|