arrays - 遍历章节名称在数组中的章节
问题描述
它需要遍历一个文档并找到章节标题(第一章、第二章等——我相信这应该是一个数组),然后在“章节”这个词之前插入一个分页符,并在章节编号之后以两个换行符结束。
与其将我的代码复制到 20 多章,不如使用数组来查找和替换术语,从而提高效率?
Sub ChapterHeadings()
'
' ChapterHeadings Macro
'
Dim Chapter As Variant
Chapter = Array("Chapter One", "Chapter Two", "Chapter Three", "Chapter Four", "Chapter Five")
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"Heading 1,Chapter Heading")
With Selection.Find
.Text = Chapter
.Replacement.Text = "^m"Chapter"^p^p"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Sub ChapterHeadings()
'
' ChapterHeadings Macro
'
'
' Copy and paste this for every chapter heading - this works!!!!!!!
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"Heading 1,Chapter Heading")
With Selection.Find
.Text = "Chapter One"
.Replacement.Text = "^mChapter One^p^p"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
查找章节标题时,它必须与 Chapter 数组中设置的内容相匹配。当有匹配时,它会用分页符 (^m) 替换结果,然后是它最初找到的章节标题,然后是两个换行符 (^p^p)。
解决方案
未经测试:
Sub ChapterHeadings()
'
' ChapterHeadings Macro
'
Dim Chapters As Variant, Chapter
Chapter = Array("Chapter One", "Chapter Two", "Chapter Three", "Chapter Four", "Chapter Five")
For Each Chapter in Chapters
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"Heading 1,Chapter Heading")
With Selection.Find
.Text = Chapter
.Replacement.Text = "^m" & Chapter & "^p^p"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next Chapter
End Sub
推荐阅读
- ios - 视图不显示和自动布局集中
- node.js - 将数据从客户端传递到服务器。表达
- google-calendar-api - 如何确定 VALarm 是否针对 CalDav 中的事件发生了变化?
- r - 如何将行中的数据组合到新列和新数据框中
- react-native - Appending two audiofiles React Native
- python - 如何使用 GraphQL 发送持久数据结构及其随时间的变化?
- ruby-on-rails - 在导入 CSV Rails 时验证唯一性而不中止更新过程
- json - 通过 NIFI 将带有 jsonb 列的记录插入 Postgres
- json - 将 OpenApi 路径拆分为多个路径定义文件
- sql - SQL - 结果来自工作日(时间)和来自星期(无时间)