分页保存-保留格式设置的代码(VBA)
2009-05-06 16:41
267 查看
功能简介:将主文档的每一页保存为一个文档,并保留中的页面设置,页眉设置,和字体样式等.
Sub SaveAsPage() Dim PageCount As Integer, StartRange As Long, EndRange As Long, MyRange As Range Dim Fn As String, MyDoc As Document, MyHeader As Range, MyFooter As Range On Error Resume Next With Selection PageCount = .Information(wdNumberOfPagesInDocument) .HomeKey unit:=wdStory For i = 1 To PageCount StartRange = .Start Set MyHeader = .Sections(1).Headers(wdHeaderFooterPrimary).Range MsgBox MyHeader MyHeader.Copy Set MyFooter = .Sections(1).Footers(wdHeaderFooterPrimary).Range MsgBox MyFooter Set MyDoc = Documents.Add '原现有光标所在页的页面设置赋值给新文档 With Application.Windows(ThisDocument.Name).Selection.Sections(1).PageSetup ActiveDocument.Sections(1).PageSetup.TopMargin = .TopMargin ActiveDocument.Sections(1).PageSetup.BottomMargin = .BottomMargin ActiveDocument.Sections(1).PageSetup.LeftMargin = .LeftMargin ActiveDocument.Sections(1).PageSetup.RightMargin = .RightMargin ActiveDocument.Sections(1).PageSetup.Orientation = .Orientation End With With ActiveDocument '打开页眉页脚 .ActiveWindow.View.SeekView = wdSeekCurrentPageHeader With Application.Windows(MyDoc).Selection .Paste '粘贴其中内容并删除最后一个段落标记 .Paragraphs(.Paragraphs.Count).Range.Delete End With '关闭页眉页脚 .ActiveWindow.View.SeekView = wdSeekMainDocument .ActiveWindow.View.Type = wdPrintView End With ThisDocument.Activate Fn = i & ActiveDocument.Name If i = PageCount Then '如果循环到达最后一页 EndRange = ActiveDocument.Content.End '将文档最后位置赋值于 EndRange Else EndRange = .GoToNext(wdGoToPage).Start '否则,将下一页的起 始位置赋值于 EndRange(等同于本页的最后位置) End If Set MyRange = ActiveDocument.Range(StartRange, EndRange) '将 本页中的内容进行复制 MyRange.Copy With Application.Windows(MyDoc).Selection .Paste .Paragraphs(.Paragraphs.Count).Range.Delete .Find.Execute findtext:="^m", Replacewith:="", Replace:=wdReplaceAll MyDoc.SaveAs FileName:=Fn '保存文档名 MyDoc.Close '关闭文档 End With Next End With End Sub '----------------------
相关文章推荐
- MyEclipse代码格式设置
- eclipase 设置代码模板,代码格式
- 用qt代码怎样编写图片保存格式[qt4.6]
- Eclipse设置、导入代码格式化模板,保存即格式化
- VC++ 截屏代码,并保存为想要的格式(BMP,JPG,PNG,GIF等格式)
- 将word文档按分页另存为多个word文件的VBA代码
- eclipse中设置代码格式
- VBA实战技巧精粹013:宏代码保存工作簿的3种方法
- 如何设置word里的代码格式,使之有底纹的效果
- Eclipse中代码格式及类和方法等注释设置及对应快捷键
- Hibernate+Mysql在向数据库中保存 / 更新数据时设置编码格式
- FlexFormatter插件,将代码设置为java格式显示方法
- VBA 第8课 Excel单元格的颜色,格式的设置
- Linux下编写C/C++明亮代码格式设置
- 如何用C#代码实现:设置系统日期格式、取硬盘的编号、取CPU编号
- 教你设置office word/excel 2007/2010默认保存格式是2003兼容格式
- Rider代码格式设置
- Eclipse设置文本格式为UTF-8/eclipse 工程平铺展开方式,如何将代码工程进行分类
- 解决使用vim-go插件时候保存go代码导致设置好的折叠消失的问题
- 给GridView设置分页格式:首页、上一页、下一页、尾页。