【问题标题】:How to set a style to parts of a document如何为文档的某些部分设置样式
【发布时间】:2019-10-31 11:03:55
【问题描述】:

我有一个函数可以为书中的标题设置样式。我正在尝试将不同的样式应用于文档的其他部分,但无法完全正常工作。

所以有3种样式:

  1. 章节标题
  2. 章节标题后的第一行文本到下一个换行符 (^p)
  3. 第一行之后的所有内容,直到下一个分页符 (^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

【问题讨论】:

    标签: arrays vba ms-word find variant


    【解决方案1】:

    您不需要通配符。一个带有循环的普通 Find 就可以了。例如:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument
      With .Range
        .Style = wdStyleNormal
        .InsertBefore vbCr
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "^pChapter"
          .Replacement.Text = ""
          .Forward = True
          .Format = False
          .Wrap = wdFindStop
          .Execute
        End With
        Do While .Find.Found
          .Start = .Start + 1
          .Paragraphs.First.Style = wdStyleHeading1
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      With .Range
        With .Find
          .Text = ""
          .Style = wdStyleHeading1
          .Execute
        End With
        Do While .Find.Found
          .Paragraphs.First.Next.Style = wdStyleHeading2
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      .Range.Characters.First.Text = vbNullString
    End With
    Application.ScreenUpdating = True
    End Sub
    

    上面的代码假定大部分文本将采用“普通”样式,而您的章节标题将采用“标题 1”样式,而后面的段落将采用“标题 2”样式。由于除了“标题 1”之外你没有说你正在使用什么样式,我只是选择了两个内置样式。

    【讨论】:

      【解决方案2】:

      如果我的理解正确,您的文档包含许多由手册分页符分隔的章节。章节标题由“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's website

      虽然它包含的许多文章都相当旧,但您可能还会发现Word MVP's 的站点很有用。它还包含指向各个 MVP 网站的链接。

      【讨论】:

      • 非常感谢您回复 m4o_tim。抱歉回复晚了,如果我能有几天的时间把所有东西都放在一起试试看,如果我有任何问题可以联系我吗?我怀疑我可能在将第一行文本设置为样式时遇到问题,而将正文的其余部分设置为另一个样式。请告诉我!
      • 只要“第一行”是一个完整的段落,正如你的问题所暗示的,你不会有任何问题。
      猜你喜欢
      • 2020-05-19
      • 2015-02-22
      • 2021-04-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多