|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
守柔版主您好!我正在处理一个邮件合并文档发,大约有195个合并邮件,每个邮件有固定格式(单个文档有10页),版面都 排好,但想把他们能分别 保存成单个文件,偶尔看到您的大作——“分页保存-保留格式设置的代码”,但我应用不成功,也不会修改,能否帮我修改一下代码让我直接能复制应用,文档名自动安顺序号保存即可,如194个文件按001,002,。。。。到194,如能赐教,本人在此不胜感激!
分页保存-保留格式设置的代码
Sub SaveAsPage()
Dim PageCount As Integer, StartRange As Long, EndRange As Long, MyRange AsRange
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'原现有光标所在页的页面设置赋值给新文档
WithApplication.Windows(ThisDocument.Name).Selection.Sections(1).PageSetup ActiveDocument.Sections(1).PageSetup.TopMargin = .TopMargin ActiveDocument.Sections(1).PageSetup.BottomMargin
= .BottomMargin
= .RightMargin
ActiveDocument.Sections(1).PageSetup.LeftMargin = .LeftMargin
ActiveDocument.Sections(1).PageSetup.RightMargin
ActiveDocument.Sections(1).PageSetup.Orientation = .Orientation
EndRange
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 '将文档最后位置赋值于
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 |
|