vb按页拆分word文档时出现宏怎么操作
下面是按页拆分word文档的程序,请参考:Option ExplicitDim oWord As Word.ApplicationDim oDoc As Word.DocumentDim oNewDoc As Word.DocumentDim oRange As Word.RangeDim iPageNumber As IntegerDim iCount As IntegerDim strTestDir As StringDim strTestFile As StringPrivate 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 < lDocumentEnd) '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 LoopEnd Sub
如何拆分word文档如何将一个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))
司机日记