VB 怎么按页拆分word文档?
下面是按页拆分word文档的程序,请参考:Option Explicit Dim oWord As Word.Application Dim oDoc As Word.Document Dim oNewDoc As Word.Document Dim oRange As Word.Range Dim iPageNumber As Integer Dim iCount As Integer Dim strTestDir As String Dim strTestFile As String Private Sub Command1_Click() Command1.Visible = False Dim lCurrentStart As Long Dim lCurrentEnd As Long Dim lDocumentEnd As Long Dim lOutputCount As Long lOutputCount = 0 'Launch Word and make it visible Set oWord = CreateObject("Word.Application") oWord.Visible = True 'Open the test document Set oDoc = oWord.Documents.Open(FileName:="C:\ThreePageDocument.doc") 'Find the beginning end of the document oDoc.Select lCurrentStart = oWord.Selection.Start lCurrentEnd = lCurrentStart lDocumentEnd = oWord.Selection.End 'Move the insertion point to the beginning of the document oWord.Selection.Collapse wdCollapseStart Do While (lCurrentEnd 'Move the insertion pointer to the bottom of this page oWord.Browser.Target = wdBrowsePage oWord.Browser.Next lCurrentEnd = oWord.Selection.End 'On the last page, the start and end will be the same If (lCurrentStart = lCurrentEnd) Then lCurrentEnd = lDocumentEnd End If 'Capture the Range of the current page Set oRange = oDoc.Range(lCurrentStart, lCurrentEnd) 'Create a new document and copy the range to it Set oNewDoc = oWord.Documents.Add oRange.Copy oNewDoc.Range(0, 0).Paste 'Release the Range so we don't leak references Set oRange = Nothing 'Save the new document and close it oNewDoc.SaveAs FileName:="C:\Result" & lOutputCount & ".doc" ' You can save as another FileFormat. If so, change the ' file extension accordingly. oNewDoc.Close Set oNewDoc = Nothing 'Increment the output counter so we don't overwrite this file later lOutputCount = lOutputCount + 1 'Reset the current start position lCurrentStart = oWord.Selection.End Loop End Sub
如何用vba按页数拆分word文档
如果想按照指定页数拆分,请使用下面的代码,其它步骤和原来那个方案相同。
Option ExplicitSub SplitEveryFivePagesAsDocuments()Dim oSrcDoc As Document, oNewDoc As DocumentDim strSrcName As String, strNewName As StringDim oRange As RangeDim nIndex As Integer, nSubIndex As Integer, nTotalPages As Integer, nBound As IntegerDim fso As ObjectConst nSteps = 100 ' 修改这里控制每隔几页分割一次Set fso = CreateObject("Scripting.FileSystemObject")Set oSrcDoc = ActiveDocumentSet oRange = oSrcDoc.ContentnTotalPages = ActiveDocument.Content.Information(wdNumberOfPagesInDocument)oRange.Collapse wdCollapseStartoRange.SelectFor nIndex = 1 To nTotalPages Step nStepsSet oNewDoc = Documents.AddIf nIndex + nSteps >nTotalPages ThennBound = nTotalPagesElsenBound = nIndex + nSteps - 1End IfFor nSubIndex = nIndex To nBoundoSrcDoc.ActivateoSrcDoc.Bookmarks("\page").Range.CopyoSrcDoc.Windows(1).ActivateApplication.Browser.Target = wdBrowsePageApplication.Browser.NextoNewDoc.ActivateoNewDoc.Windows(1).Selection.PasteNext nSubIndexstrSrcName = oSrcDoc.FullNamestrNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _fso.GetBaseName(strSrcName) & "_" & (nIndex \ nSteps + 1) & "." & fso.GetExtensionName(strSrcName))oNewDoc.SaveAs strNewNameoNewDoc.Close FalseNext nIndexSet oNewDoc = NothingSet oRange = NothingSet oSrcDoc = NothingSet fso = NothingMsgBox "结束!"End Sub
如何将一个word文档按页拆分成多个文档
1、在Word里面打开那个需要分割的文档(假设它的文件名叫做“原始文档.doc”);键入ALT+F11打开VBA编辑器,选择菜单“插入-模块”;粘贴下面的代码:Option ExplicitSub SplitPagesAsDocuments()Dim oSrcDoc As Document, oNewDoc As DocumentDim strSrcName As String, strNewName As StringDim oRange As RangeDim nIndex As IntegerDim fso As ObjectSet fso = CreateObject("Scripting.FileSystemObject")Set oSrcDoc = ActiveDocumentSet oRange = oSrcDoc.ContentoRange.Collapse wdCollapseStartoRange.SelectFor nIndex = 1 To ActiveDocument.Content.Information(wdNumberOfPagesInDocument)oSrcDoc.Bookmarks("\page").Range.CopyoSrcDoc.Windows(1).ActivateApplication.Browser.Target = wdBrowsePageApplication.Browser.NextstrSrcName = oSrcDoc.FullNamestrNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _fso.GetBaseName(strSrcName) & "_" & nIndex & "." & fso.GetExtensionName(strSrcName))Set oNewDoc = Documents.AddSelection.PasteoNewDoc.SaveAs strNewNameoNewDoc.Close FalseNextSet oNewDoc = NothingSet oRange = NothingSet oSrcDoc = NothingSet fso = NothingMsgBox "结束!"End Sub键入F5运行,看到“完成!”结束。
2、检查当前文档所在路径下是否生成若干名为“原始文档_n.doc”(n代表其对应原始文档中的第几页)的文档,检查它们的内容是否就对应于原始文档每个页面的内容。
如文档中有分节符分解后的文档会出现空白页,如要分解后不出现空白页,需要把文档中的分节符删除。
消除分节符的方法:注意事项分节符若全部替换,要注意替换后文档可能会出现排版混乱,这则需要自己手动排版了。
如何将一个word文档按页拆分成多个文档
Target = wdBrowsePage Application.Browser.Next strSrcName = oSrcDoc.FullName strNewName = fso; & fso.GetExtensionName(strSrcName)) Set oNewDoc = Documents,需要把文档中的分节符删除。
消除分节符的方法。
如文档中有分节符分解后的文档会出现空白页.BuildPath(fso.GetBaseName(strSrcName) & ",如要分解后不出现空白页;\page").Range.Copy oSrcDoc.Windows(1).doc”(n代表其对应原始文档中的第几页)的文档,检查它们的内容是否就对应于原始文档每个页面的内容;_" & nIndex & ", _ fso."结束!"End Sub键入F5运行,看到“完成!”结束。
2、检查当前文档所在路径下是否生成若干名为“原始文档_n.Add Selection.Paste oNewDoc.SaveAs strNewName oNewDoc.Close False Next Set oNewDoc = Nothing Set oRange = Nothing Set oSrcDoc = Nothing Set fso = Nothing MsgBox ".GetParentFolderName(strSrcName), 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.Browser.Activate Application.Select For nIndex = 1 To ActiveDocument.Content.Information(wdNumberOfPagesInDocument) oSrcDoc.Bookmarks("1、在Word里面打开那个需要分割的文档(假设它的文件名叫做“原始文档.doc”);键入ALT+F11打开VBA编辑器,选择菜单“插入-模块”;粘贴下面的代码:Option ExplicitSub SplitPagesAsDocuments()Dim oSrcDoc As Document, oNewDoc As DocumentDim strSrcName As String
如何拆分word文档如何将一个word文档中某页其中的一段拆分为
1、在Word里面打开那个需要分割的文档(假设它的文件名叫做“原始文档.doc”);键入ALT+F11打开VBA编辑器,选择菜单“插入-模块”;粘贴下面的代码:Option ExplicitSub SplitPagesAsDocuments()Dim oSrcDoc As Document, oNewDoc As DocumentDim 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 Select...
有很多WORD文档,每个文档有2到10页不等,想要把所有WORD都按...
等候几分钟.Bookmarks(".新建一个WORD文档,命名后保存到和要合并的1000多篇文档一个文件夹里。
2.Activate Application, strNewName As String Dim oRange As Range Dim nIndex As Integer;Scripting;2,请使用下面的代码,其它步骤和原来那个方案相同。
Option ExplicitSub SplitEveryFivePagesAsDocuments() Dim oSrcDoc As Document, oNewDoc As Document Dim strSrcName As String.Path MyName = Dir(MyPath & "\, nTotalPages As Integer.Paste i = i + 1 wb.Close False End If MyName = Dir Loop Application,即可完成合并1.Content.WholeStory Selection;_" & nIndex & ".Copy Windows(1).Activate Selection.EndKey Unit:=wdLine Selection, 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(" & fso.GetExtensionName(strSrcName)) Set oNewDoc = Documents.FileSystemObject"、恩ALT+F11进入宏编辑器。
3;结束!"End Sub4, nSubIndex As Integer;".doc”(n代表其对应原始文档中的第几页)的文档,检查它们的内容是否就对应于原始文档每个页面的内容."、在Word里面打开那个需要分割的文档(假设它的文件名叫做“原始文档.Information(wdNumberOfPagesInDocument) oSrcDoc供你参考; "" If MyName <.Name Then Set wb = Documents.Open(MyPath & "\.Content oRange;>".ScreenUpdating = True End Sub 4、运行; & MyName) Selection;3、双击左上角的ThisDocument,在右侧的代码区粘贴代码如下: Sub 合并文档() Application.ScreenUpdating = False MyPath = ActiveDocument:1.GetBaseName(strSrcName) & ".FullName strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _ fso.TypeParagraph Selection; ActiveDocument;\page").Range.Copy oSrcDoc.Windows(1).SaveAs strNewName oNewDoc.Close False Next Set oNewDoc = Nothing Set oRange = Nothing Set oSrcDoc = Nothing Set fso = Nothing MsgBox "、键入F5运行,看到“完成!”结束。
5、检查当前文档所在路径下是否生成若干名为“原始文档_n, nBound As Integer Dim fso As Object Const nSteps = 200 ' 修改这里控制每隔几页分割一次 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 + 1) & "." & 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。
不过那个是按单页拆分的.Target = wdBrowsePage Application.Browser.Next strSrcName = oSrcDoc。
如果想按照指定页数拆分:Option ExplicitSub SplitPagesAsDocuments() Dim oSrcDoc As Document、键入ALT+F11打开VBA编辑器.doc”)、粘贴下面的代码.Add Selection.Paste oNewDoc;) Set oSrcDoc = ActiveDocument Set oRange = oSrcDoc;) i = 0 Do While MyName <.Collapse wdCollapseStart oRange.Select For nIndex = 1 To ActiveDocument.Browser;>,选择菜单“插入-模块”; & "*.doc" 展开
转载请注明出处51数据库 » wordvba按页拆分文档
珊珊珊珊珊萌