下面代碼把原文檔按照指定的標識(這里是“END”)進行拆分。拆分之后的文檔生成在源文檔當前目錄下,文件名后面附加來自“_1”、“_2”、“_3”等。 Optio360問答n Explicit Const Token = 'END' Sub SplitDocumentByToken() Dim o果段絕資NewDoc As Docum己乙六米景永不ent Dim strSrcName As String, strNewName As String Dim nStart As Integer, nEnd As Integer, nIndex As Integer Dim fContinue As 門穿取揚鮮富及辦特Boolean Dim fso As Object Set fso = CreateObject('Scripting.FileSystemObject') strSrcName = ActiveDocument.FullName nIndex = 1 fContinue = True Selection.StartOf WdUnits.wdStory Do While fContinue nStart = Selection.Start Selection.臺位密機械科靜至啊Find.ClearForma燈趕作連服界號tting With Selection.Find .Text = '^13' & Token & '^13' .Replacement.Text = '' .副擔連精然或羅相危蘭從Forward = True .W書擴想rap = WdFindW也章靜普粉rap.wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With If Selection.Find.Execute Then nEnd = Selection.End Else nEnd = ActiveDocument.Content.End fContinue = False End If ActiveDocument.Range(nStart, nEnd).Copy strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _ fso.GetBaseName(strSrcName) & '_' & nIndex & '.' & fso.GetExtensionName(strSrcName)) Set oNewDoc = Docu茶ments.Add Selection.Paste oN宗電優(yōu)若得無低容房改ewDoc.SaveAs strNewName oNewDoc.Clos零良品批商章布e False nIndex = nIndex + 1 Selection.Collapse WdCollap占seDirection.wdCollapseEnd Lo標松善終題編被屋安op Set oNewD限oc = Nothing Set fso = Nothing MsgBox '結(jié)束!' End Sub |
|