狂人呓语

Word宏 自动将多页面拆分成多个单页文件

1、在Word里面打开那个需要分割的文档(假设它的文件名叫做“原始文档.doc”);
键入ALT+F11打开VBA编辑器,选择菜单“插入-模块”;
粘贴下面的代码:

Option Explicit
Sub SplitPagesAsDocuments()
Dim oSrcDoc As Document, oNewDoc As Document
Dim strSrcName As String, strNewName As String
    Dim oRange As Range
    Dim nIndex As Integer
    Dim fso As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oSrcDoc = ActiveDocument
    Set oRange = oSrcDoc.Content
    oRange.Collapse wdCollapseStart
    oRange.Select
    For nIndex = 1 To ActiveDocument.Content.Information(wdNumberOfPagesInDocument)
       oSrcDoc.Bookmarks("\page").Range.Copy
        oSrcDoc.Windows(1).Activate
        Application.Browser.Target = wdBrowsePage
        Application.Browser.Next
       
        strSrcName = oSrcDoc.FullName
        strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
                    fso.GetBaseName(strSrcName) & "_" & nIndex & "." & fso.GetExtensionName(strSrcName))
        Set oNewDoc = Documents.Add
        Selection.Paste
        oNewDoc.SaveAs strNewName
        oNewDoc.Close False
    Next
    Set oNewDoc = Nothing
    Set oRange = Nothing
    Set oSrcDoc = Nothing
    Set fso = Nothing
    MsgBox "结束!"
End Sub

键入F5运行,看到“完成!”结束。
2、检查当前文档所在路径下是否生成若干名为“原始文档_n.doc”(n代表其对应原始文档中的第几页)的文档,检查它们的内容是否就对应于原始文档每个页面的内容。

未经允许不得转载:狂人呓语 » Word宏 自动将多页面拆分成多个单页文件

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址