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这段代码依然在第一行显示未声明变量
时间: 2023-11-27 19:51:24 浏览: 112
这段代码中的错误是因为缺少了 "Option Explicit" 语句。可以在代码的最上面添加 "Option Explicit",它可以强制要求在使用变量之前必须先声明它们,从而防止因为未声明变量而导致的错误。
修改后的代码如下:
```
Option Explicit
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
```
请尝试运行一下看看是否还有其他问题。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)