arrays - 如何为文档的某些部分设置样式
问题描述
我有一个功能可以为书中的标题设置样式。我正在尝试将不同的样式应用于文档的其他部分,但无法完全正常工作。
所以有3种风格:
- 章节标题
- 章节标题后的第一行文本到下一个换行符 (^p)
- 第一行之后的所有内容,直到下一个分页符 (^m)
我确定我使用通配符向其他数组声明 - 但在逻辑上遇到困难,并且在通配符 = true 时遇到问题。
如果有人也碰巧有任何关于如何设置自己的样式的资源,然后将它们声明到每个部分(请参阅代码中的替换样式以了解我想要做什么 - “标题 1,章节标题” - 我可以在宏中定义它?),我真的很感激!
有关章节功能,请参见下面的代码。
Private Function iiiChapterHeadings()
Dim Chapters As Variant, Chapter
Chapters = Array("Chapter One", "Chapter Two", "Chapter Three")
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
End Function
文件示例!!!!
Chapter One^p
^p
This first line of text will have a style set to it so there's no indentation, the first letter of the sentence is a capital letter and it applies to everything up to the next line break.^p
Everything after the line break above has a different style set to it.^p
All of this will have that style up until the next page break.^p
As will this line.^p
And this etc. ^p
^m
Chapter Two^p
^p
解决方案
如果我理解正确,您的文档包含许多由手册分页符分隔的章节。章节标题由“Chapter”和另一个单词组成,后面是一个空段落。
在您的问题中,您指的是“换行符(^p)”。注意不要将行与段落混淆,因为它们是不同的。^p 实际上是一个段落标记,使用Enter
键输入。Word 中也有手动换行符,使用 输入Shift+Enter
,使用 ^l 找到。
如果分页符的唯一目的是确保章节标题在新页面上,则没有必要。您的章节标题样式可以通过设置“Page break before”来定义为具有隐式分页符。
章节标题后的空白段落也是不必要的。打字机不再需要这种间隔方法。而是为您的章节标题样式设置“间距”以提供适当的空间。
Word 的内置样式(普通除外)都有描述其预期用途的名称。最适合文档正文中文本的样式是正文。
由于 Word 的内置样式名称会更改以反映应用程序语言,因此在引用它们时最好使用 WdBuiltinStyle 枚举器。尽管缺少最近添加的样式,但大多数样式都有价值。
下面的代码完成了上述所有操作,并使用通配符搜索来避免向文档添加任何额外内容。
Sub FormatDocument()
With ActiveDocument.Range
'set base style for document
.Style = wdStyleBodyText
RemovePageBreaks
ModifyHeading1
' Find chapter headings and apply Heading 1
With .Find
.ClearFormatting
.Text = "Chapter <*>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
With .Paragraphs.First
.Style = wdStyleHeading1
.Next.Style = wdStyleHeading2
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Sub
Private Sub RemovePageBreaks()
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^m"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
'removing the page break leaves an empty para so remove all empty paragraphs
'this will also remove the unnecessary empty paragraph after the chapter heading
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p^p"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
End Sub
Private Sub ModifyHeading1()
With ActiveDocument.Styles(wdStyleHeading1).ParagraphFormat
'add implicit page break
.PageBreakBefore = True
'add space after in points
.SpaceAfter = 12
End With
End Sub
如果您想了解更多关于 Word 样式的信息,请访问Shauna Kelly 的网站
尽管它包含的许多文章都相当古老,但您可能还会发现Word MVP 的站点很有用。它还包含指向各个 MVP 网站的链接。
推荐阅读
- r - 仅当我将包裹提交给 CRAN 时才会出错
- javascript - 顺序回调的javascript练习
- javascript - 秘银组件如何在这里工作是我的一些奇怪的经历
- kotlin - Tornadofx:在 runAsync 任务期间更新标签文本 (FXML)
- sql - 使用实体框架迁移转换 SQL 列的类型
- highcharts - 如何避免 Webdatarocks 过滤器功能以避免在图表和数据透视表中链接过滤器
- lets-encrypt - 如何让 Traefik 信任我们公司的 CA 来生成 letencrypt 证书?
- javascript - 如何借助 RecordRTC 在 JavaScript 中创建 WAV 流?
- postgresql - 如何从 postgres 分组集查询中删除空值
- node.js - mongoose find() 文档具有大小 > 0 的属性数组