分页保存为新文档

时间:2022-09-06 05:39:49

' WORD功能。如何将一个多页文档分别每页保存为多个单页文档。以下为代码

Sub 分页保存为新文档()
    '分页保存,适用于WORD97及其以上版本
    Dim objShell As Object, objFolder As Object, strNameLenth As Integer
    Dim mySelection As Selection, myfolder As String, myArray() As String
    Dim ThisDoc As Document, myDoc As Document, strName As String, n As Integer
    Dim myRange As Range, PageString As String, pgOrientation As WdOrientation
    Dim sinLeft As Single, sinRight As Single, sinTop As Single, sinBottom As Single
    Dim ErrChar() As Variant, ochar As Variant, sinStart As Single, sinEnd As Single
    Const myMsgTitle As String = "ExcelHome_ShouRou"
    Dim vbYN As VbMsgBoxResult
    sinStart = Timer
    On Error GoTo errhandle    '设置错误处理
    '创建一个Shell.Application对象
    Set objShell = CreateObject("Shell.Application")
    '取得文件夹浏览器
    Set objFolder = objShell.BrowseForFolder(0, "请选择一个文件夹", 0, 0)
    If objFolder Is Nothing Then Exit Sub
    myfolder = objFolder.Self.Path & "\"
    Set objFolder = Nothing: Set objShell = Nothing
    Set ThisDoc = ActiveDocument    '定义一个Document对象,以利用本程序作为加载宏
    Set mySelection = ThisDoc.ActiveWindow.Selection
    '文件自动命名时必须规避的字符
    ErrChar = Array("\", "/", ":", "*", "?", """", "<", ">", "|")
    '一些特列字符
    For n = 0 To 31
        ReDim Preserve ErrChar(UBound(ErrChar) + 1)
        ErrChar(UBound(ErrChar)) = Chr(n)
    Next
    strNameLenth = Val(VBA.InputBox(Prompt:="请输入您需要设置的文件名长度,0或者取消将自动命名!", Title:=myMsgTitle, Default:=10))
    If strNameLenth > 255 Then strNameLenth = 0
    vbYN = MsgBox("是否需要处理页尾的分隔符(分页符/分节符)?它可能会影响文档结构.", vbYesNo + vbInformation + vbDefaultButton2, myMsgTitle)
    Application.ScreenUpdating = False    '关闭屏幕更新
    '在文档的每页中循环
    For n = 1 To mySelection.Information(wdNumberOfPagesInDocument)
        mySelection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=n
        Set myRange = ThisDoc.Bookmarks("\PAGE").Range
        If vbYN = vbYes And VBA.Asc(myRange.Characters.Last.Text) = 12 Then _
           myRange.SetRange myRange.Start, myRange.End - 1
        '取得一个以段落标记为分隔符的一维数组
        myArray = VBA.Split(myRange.Text, Chr(13))
        '将所有文本合并为一个字符串
        PageString = VBA.Join(myArray, "")
        '取得文档中每节的页面设置
        With myRange.Sections(1).PageSetup
            sinLeft = .LeftMargin    '左页边距
            sinRight = .RightMargin    '右页边距
            sinTop = .TopMargin    '上边距
            sinBottom = .BottomMargin    '下边距
            pgOrientation = .Orientation    '纸张方向
        End With
        For Each ochar In ErrChar    '进行一系列替换,即删除无效字符
            PageString = VBA.Replace(PageString, ochar, "")
        Next
        If strNameLenth = 0 Then
            strName = ThisDoc.Name
            strName = VBA.Replace(LCase(strName), ".doc", "")
            strName = strName & "_" & n
        Else
            strName = VBA.Left(PageString, strNameLenth)    '取得文件名
            If strName = "" Then
                strName = ThisDoc.Name
                strName = VBA.Replace(LCase(strName), ".doc", "")
                strName = strName & "_" & n
            End If
        End If
        strName = strName & ".doc"
        myRange.Copy    '复制
        Set myDoc = Documents.Add(Visible:=True)    '新建一个隐藏的空白文档
        With myDoc
            With .Content
                .Paste    '粘贴
                .Paragraphs.Last.Range.Delete    '删除最后一个段落标记
                With .Find
                    .ClearFormatting
                    .Text = "^b"
                    .Replacement.ClearFormatting
                    .Replacement.Text = ""
                    .Execute Replace:=2
                End With
            End With
            With .PageSetup    '进行页面设置
                .Orientation = pgOrientation
                .LeftMargin = sinLeft
                .RightMargin = sinRight
                .TopMargin = sinTop
                .BottomMargin = sinBottom
            End With
            '如果有相同的文档,则自动命名
            If VBA.Dir(myfolder & strName, vbDirectory) <> "" Then strName = "Page_" & n & ".doc"
            .SaveAs myfolder & strName    '另存为
            .Close    '关闭文档
        End With
    Next
    ThisDoc.Characters(1).Copy    '变相清空剪贴板
    Application.ScreenUpdating = True    '恢复屏幕更新
    sinEnd = Timer    '取得代码运行结束的时间
    If MsgBox("分页保存结束,用时:" & sinEnd - sinStart & _
              "秒,是否打开指定文件夹查看分页保存后的文档情况?", vbYesNo, myMsgTitle) = vbYes Then _
       ThisDoc.FollowHyperlink myfolder
    Exit Sub
errhandle:
    MsgBox "错误号:" & Err.Number & vbLf & "出错原因:" & Err.Description, myMsgTitle
    Err.Clear
    Application.ScreenUpdating = True    '恢复屏幕更新
End Sub