快捷搜索:  汽车  科技

vba批量打印word文档(Word文档利用VBA代码快速将一个文档按需分为多个文档)

vba批量打印word文档(Word文档利用VBA代码快速将一个文档按需分为多个文档)Dim nIndex As IntegerDim oRange As RangeSub SplitPagesAsDocuments()Dim oSrcDoc As Document oNewDoc As DocumentDim strSrcName As String strNewName As String

日常工作中,我们偶尔会遇到需要将一个分工方案或某个文档,按章节分为多个文档,以便直接将不同内容直接分给不同责任人。平时大家是否是一个一个复制粘贴的呢?这样当然可行,但是下面这个方法会让你意想不到的简单,并且结合我们之前分享的快速提取提纲(附注1)和批量重命名(附注2)功能,能够瞬间(夸张的说法)完成所有操作。

vba批量打印word文档(Word文档利用VBA代码快速将一个文档按需分为多个文档)(1)

图片来自网络


实例演示

一、源代码

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

二、操作演示

首先,根据自己的需求,将需要切割的文本处插入分页符(可使用高级替换功能批量插入哟)→然后选择开发工具→Visual Basic→插入→模块→粘贴以上代码→点击上方的三角形运行即可。

vba批量打印word文档(Word文档利用VBA代码快速将一个文档按需分为多个文档)(2)

完成后

动图演示

vba批量打印word文档(Word文档利用VBA代码快速将一个文档按需分为多个文档)(3)

动图演示


小结

此方法非常简单易学,结合之前给大家分享的快速批量重命名文件,应该能够大大提高工作效率!如果需要单独获取源代码的,敬请下方留言!

如果对您有所帮助,感谢点赞!关注获取更多电脑或Office经验分享!

附注

1.实例分享:Word文档快速提取提纲或将提纲标题快速分为很多页

2.实例分享:bat牵手Excel,运用ren命令批量对文件名进行重命名

猜您喜欢: