首页 > 其他 > 详细

利用VBA拆分Word每个页面并分别保存

时间:2014-01-23 16:24:52      阅读:398      评论:0      收藏:0      [点我收藏+]

Word中Mail Merge功能之后,分别保存成独立的word文件

 

bubuko.com,布布扣
Sub BreakOnSection()
Dim a As Excel.Application, ab As Excel.Workbook
Set a = CreateObject("excel.application")
Set ab = a.Workbooks.Open("D:\Book2.xlsx")
  Application.ScreenUpdating = False Makes the code run faster and reduces screen flicker a bit.

   Used to set criteria for moving through the document by section.
  Application.Browser.Target = wdBrowseSection
  strBaseFilename = ActiveDocument.Name
  On Error GoTo CopyFailed

  A mail merge document ends with a section break next page.
  For I = 1 To ActiveDocument.Sections.Count

       Select and copy the section text to the clipboard.
       ActiveDocument.Bookmarks("\Page").Range.Copy

       Create a new document to paste text from clipboard.
       Documents.Add
       Selection.Paste
       DocNum = DocNum + 1
       With ab
           empId = .Sheets(1).Range("a" & DocNum + 1)
           pwd = .Sheets(1).Range("c" & DocNum + 1)
       End With
           strNewFileName = "Salary" & empId
           ActiveDocument.SaveAs "D:\" & strNewFileName, Password:=CStr(pwd), WritePassword:="admin112233", ReadOnlyRecommended:=True
           ActiveDocument.Close
            Move the selection to the next section in the document.
           Application.Browser.Next
       Next I
           Application.Quit SaveChanges:=wdSaveChanges
       End
CopyFailed:
   MsgBox ("No final Section Break in " & strBaseFilename)
   Application.Quit SaveChanges:=wdSaveChanges
   End
End Sub
bubuko.com,布布扣

利用VBA拆分Word每个页面并分别保存

原文:http://www.cnblogs.com/batter152/p/3530711.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!