如何用vba把word文档的每页单独保存为一个word文档?

要将word文档的每页单独保存为一个word文档,首先需要能够逐页遍历word文档。

逐页遍历word文档可以使用word的定位功能。

代码如下:

Sub QQ1722187970()
    Const wdNumberOfPagesInDocument = 4
    Const wdGoToPage = 1
    Const wdGoToAbsolute = 1
    Dim oDoc As Document
    Dim oRng As Range
    Dim oDocTemp As Document
    Set oDoc = Word.ActiveDocument
    Dim sPath As String
    sPath = Word.ActiveDocument.Path
    Dim iPageNo As Long
    '获取总页数
    With oDoc
    iPageNo = .Range.Information(wdNumberOfPagesInDocument)
        For I = 1 To iPageNo
            '定位到页开始
            Set oRng = .GoTo(wdGoToPage, Which:=wdGoToAbsolute, Count:=I)
            oRng.Select
            '定位整个页面区域
            oRng.SetRange oRng.Start, oRng.Bookmarks("\page").End
            oRng.Copy
            Set oDocTemp = Word.Documents.Add
            With oDocTemp.Application.Selection
                .Paste
            End With
            oDocTemp.SaveAs2 sPath & "\" & I & ".docx"
            oDocTemp.Close
        Next I
    End With
End Sub

 

       

发表评论