|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub 按标题拆分文档()
- Application.ScreenUpdating = False
- Dim arr()
- Dim srr()
- Dim drr()
- Dim old
- Dim ioo
- Dim ppt
- Dim wdfullname
- Dim wdpath
- Dim i As Integer
- Dim wd As Document
- ioo = MsgBox("是否另存为pdf", vbYesNoCancel + vbInformation)
- If ioo = 2 Then Exit Sub
- Set wd = ActiveDocument
- wdpath = ActiveDocument.Path
- wdfullname = ActiveDocument.fullname
- drr() = Array("/", "", ":", "*", "?", """", "<", ">", "|", ChrW(13))
- Selection.HomeKey Unit:=wdStory
- Selection.Find.Style = "标题 1"
- Do While Selection.Find.Execute
- n = n + 1
- ReDim Preserve arr(1 To n)
- ReDim Preserve srr(1 To n)
- arr(n) = Selection.Range.Text
- For Each old In drr()
- arr(n) = Replace(arr(n), old, "")
- Next
- srr(n) = Selection.Information(wdActiveEndPageNumber)
- Loop
- For i = 1 To n
- If Dir(wdpath & "" & arr(i) & ".docx") <> "" Then
- MsgBox "当前路径存在和标题" & arr(i) & "重名的word文件,即将退出"
- Selection.HomeKey Unit:=wdStory
- Exit Sub
- End If
- If ioo = 6 Then
- If Dir(wdpath & "" & arr(i) & ".pdf") <> "" Then
- MsgBox "当前路径存在和标题" & arr(i) & "重名的pdf文件,即将退出"
- Selection.HomeKey Unit:=wdStory
- Exit Sub
- End If
- End If
- Next
- If MsgBox("是否先将当前文档保存一下", vbYesNo + vbInformation) = 6 Then ActiveDocument.Save
- For i = 1 To n
- wd.SaveAs2 wdpath & "" & arr(i) & ".docx"
- Next
- wd.Close
- For i = 1 To n
- Documents.Open wdpath & "" & arr(i) & ".docx"
- If i <> n Then
- Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=srr(i + 1)
- Selection.EndKey Unit:=wdStory, Extend:=wdExtend
- Selection.Delete
- End If
- If srr(i) <> 1 Then
- Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=srr(i)
- Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
- Selection.Delete
- End If
- If ioo = 6 Then ActiveDocument.SaveAs2 wdpath & "" & arr(i), 17
- ActiveDocument.Close SaveChanges:=wdSaveChanges
- Next
- Set wd = Nothing
- If Documents.Count = 0 Then Application.Quit
- Application.ScreenUpdating = True
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|