按指定页数动态拆分Word文档,这样的Word拆分真是太实用了!
Excel情报局
Excel职场联盟
前言|职场实例
在文件夹内打开那个需要按页拆分的文档(本例中它的文件名叫做“Excel情报局 总表”)
Option Explicit
Sub DynamicSplitPagesAsDocuments()
Dim oSrcDoc As Document, oNewDoc As Document
Dim strSrcName As String, strNewName As String
Dim oRange As Range
Dim nIndex As Integer, nSubIndex As Integer, nTotalPages As Integer, nBound As Integer
Dim fso As Object
Const nSteps = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set oSrcDoc = ActiveDocument
Set oRange = oSrcDoc.Content
nTotalPages = ActiveDocument.Content.Information(wdNumberOfPagesInDocument)
oRange.Collapse wdCollapseStart
oRange.Select
For nIndex = 1 To nTotalPages Step nSteps
Set oNewDoc = Documents.Add
If nIndex + nSteps > nTotalPages Then
nBound = nTotalPages
Else
nBound = nIndex + nSteps - 1
End If
For nSubIndex = nIndex To nBound
oSrcDoc.Activate
oSrcDoc.Bookmarks("\page").Range.Copy
oSrcDoc.Windows(1).Activate
Application.Browser.Target = wdBrowsePage
Application.Browser.Next
oNewDoc.Activate
oNewDoc.Windows(1).Selection.Paste
Next nSubIndex
strSrcName = oSrcDoc.FullName
strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
fso.GetBaseName(strSrcName) & "_" & (nIndex \ nSteps) & "." & fso.GetExtensionName(strSrcName))
oNewDoc.SaveAs strNewName
oNewDoc.Close False
Next nIndex
Set oNewDoc = Nothing
Set oRange = Nothing
Set oSrcDoc = Nothing
Set fso = Nothing
MsgBox "结束!"
End Sub
最后效果截图:
按指定页数动态拆分Word文档,按每2页拆分了“Excel情报局 总表”Word文档,最后形成2个独立的Word文档,每个文档中显示2页,每页为一首宋词内容。
重点代码注释:
代码是固定的模板,只有下面一句需要根据现实办公需求灵活修改:
Const nSteps = 2
这里可以指定需要拆分的页数,如这里表示按照每2页拆分成一个小文档,依此类推,如果按照每3页拆分成一个小文档,即可修改为:
Const nSteps = 3
赞 (0)