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

本文介绍如何在Word中使用MailMerge功能批量生成独立的带密码保护的Word文档,并遵循特定的命名规范。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 并且添加密码,增加命名规范

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 = "xxxx" & empId
           ActiveDocument.SaveAs "D:\" & strNewFileName, Password:=CStr(pwd), WritePassword:="xxxxx", 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

 

转载于:https://www.cnblogs.com/batter152/p/3530711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值