/** * Katana1994 Macro */ function Katana1994() { Dim doc1 As Document Dim rng As Range Dim chapterStart As Long Dim chapterEnd As Long Dim chapterTitle As String Dim chapterIndex As Integer Dim newDoc As Document Set doc1 = ActiveDocument chapterIndex = 1 ' 遍历文档的每个段落 For i = 1 To doc.Paragraphs.Count Set rng = doc.Paragraphs(i).Range ' 检查段落是否为章节标题 If rng.Text Like "第*篇*" Or rng.Text Like "*、*" Then If chapterStart > 0 Then chapterEnd = rng.Start - 1 ' 选中章节内容 doc.Range(chapterStart, chapterEnd).Select ' 复制章节内容 Selection.Copy ' 创建新文档并粘贴章节内容 Set newDoc = Documents.Add newDoc.Content.Paste ' 保存新文档 newDoc.SaveAs "Chapter" & chapterIndex & " - " & chapterTitle & ".docx" newDoc.Close chapterIndex = chapterIndex + 1 End If ' 更新章节标题和起始位置 chapterTitle = rng.Text chapterStart = rng.End End If Next i } 纠正代码
时间: 2023-11-27 13:51:16 浏览: 84
katana-sdk-php7:用于KATANA框架PHP7 SDK
这段代码有一些错误,需要进行一些修改:
1. 定义的过程名为 Katana1994(),但是在代码中使用了 doc 变量,应该将其改为 doc1。
2. 在 For 循环中,应该使用 doc1.Paragraphs.Count 而不是 doc.Paragraphs.Count。
3. 在 If 语句中,应该使用 rng.Text Like "第*篇*" 或者 rng.Text Like "*、*",而不是将它们写在一起。
4. 在创建新文档时,应该使用 Documents.Add() 而不是 Documents.Add。
5. 在保存新文档时,应该将文件名中的章节标题改为 chapterTitle。
修改后的代码如下:
```
Sub Katana1994()
Dim doc1 As Document
Dim rng As Range
Dim chapterStart As Long
Dim chapterEnd As Long
Dim chapterTitle As String
Dim chapterIndex As Integer
Dim newDoc As Document
Set doc1 = ActiveDocument
chapterIndex = 1
For i = 1 To doc1.Paragraphs.Count
Set rng = doc1.Paragraphs(i).Range
If rng.Text Like "第*篇*" Or rng.Text Like "*、*" Then
If chapterStart > 0 Then
chapterEnd = rng.Start - 1
doc1.Range(chapterStart, chapterEnd).Select
Selection.Copy
Set newDoc = Documents.Add()
newDoc.Content.Paste
newDoc.SaveAs "Chapter" & chapterIndex & " - " & chapterTitle & ".docx"
newDoc.Close
chapterIndex = chapterIndex + 1
End If
chapterTitle = rng.Text
chapterStart = rng.End
End If
Next i
End Sub
```
阅读全文