为整个文档格式化每章第一段的前四个词

Formatting first four words in first paragraph of each chapter for the entire document

我在 MS Word 中格式化我的手稿以便出版。我想格式化每章的第一段

下面需要我去每个第一段和select我想变成的词'small caps'

With Selection.Find
    .Text = "[a-z]{1,}"
    .MatchWildcards = True
    .MatchCase = True
    .Replacement.Font.Size = 9.5
    .Execute Replace:=wdReplaceAll, Forward:=True, _
      Wrap:=wdFindStop
End With
Selection.Range.Case = wdUpperCase
With Selection.ParagraphFormat
    .LeftIndent = InchesToPoints(0)
    .RightIndent = InchesToPoints(0)
    .SpaceBefore = 0
    .SpaceBeforeAuto = False
    .SpaceAfter = 0
    .SpaceAfterAuto = False
    .LineSpacingRule = wdLineSpaceMultiple
    .LineSpacing = LinesToPoints(1)
    .Alignment = wdAlignParagraphJustify
    .WidowControl = True
    .KeepWithNext = False
    .KeepTogether = False
    .PageBreakBefore = False
    .NoLineNumber = False
    .Hyphenation = True
    .FirstLineIndent = InchesToPoints(0)
    .OutlineLevel = wdOutlineLevelBodyText
    .CharacterUnitLeftIndent = 0
    .CharacterUnitRightIndent = 0
    .CharacterUnitFirstLineIndent = 0
    .LineUnitBefore = 0
    .LineUnitAfter = 0
    .MirrorIndents = False
    .TextboxTightWrap = wdTightNone
    .CollapsedByDefault = False
End With

我正在寻找一个版本,它会自动 select 整个文档每章第一段的前四个词。

第一段的风格与 Word 'Normal' 风格截然不同。

有一个分页符和一个使用标题 1 的章节标题。

假设每一章都以 Heading 1 样式的标题开始......并且假设我正确理解下一段(本章的第一段)你希望前四个词是小型大写字母......这应该可以它:

Sub HeadingOneRange()
    Dim doc As Word.Document, i As Long
    Dim rng As Word.Range, iRng As Range
    
    Set doc = ActiveDocument
    Set rng = doc.Content
    Application.ScreenUpdating = False
    On Error GoTo errHandler
    
    With rng.Find
        .ClearFormatting
        .Format = True
        .Forward = True
        .Style = doc.Styles("Heading 1").NameLocal
        .Text = ""
        .Wrap = wdFindStop
        While .Execute
            rng.Select
            Set iRng = rng.Bookmarks("\HeadingLevel").Range.Paragraphs(2).Range
            With iRng
                .Collapse Word.WdCollapseDirection.wdCollapseStart
                .MoveEnd Word.WdUnits.wdWord, Count:=4
                For i = 1 To .Characters.Count
                    If .Characters(i).Case = wdLowerCase Then
                        .Characters(i).Font.Size = 9.5
                        .Characters(i).Case = wdUpperCase
                    End If
                Next
            End With
            rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
        Wend
    End With
    Selection.HomeKey wdStory
errHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Number & vbCr & Err.Description, vbCritical
        Err.Clear
    End If
    Application.ScreenUpdating = True
    MsgBox "Action Complete"
End Sub

更好的方法是使用您要使用的属性定义段落和字符样式,然后将这些样式应用到相关内容。蛮力格式更改会增加文档损坏的可能性。尝试以下方法:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
On Error Resume Next
With ActiveDocument
  'Create a paragraph Style for content following Heading1
  .Styles.Add Name:="Head1Next", Type:=wdStyleTypeParagraph
  With .Styles("Head1Next")
    With .ParagraphFormat
      .Alignment = wdAlignParagraphJustify
      .LeftIndent = 0
      .RightIndent = 0
      .SpaceAfter = 0
      .SpaceBefore = 0
      .WidowControl = True
      .KeepWithNext = False
      .KeepTogether = False
      .PageBreakBefore = False
    End With
    'With .Font
    '  .Name = "Arial"
    '  .Size = 12
    'End With
  End With
  'Create a charcter Style for Small Caps
  .Styles.Add Name:="SmallCaps", Type:=wdStyleTypeCharacter
  With .Styles("SmallCaps").Font
    .AllCaps = True
    .Size = 9.5
  End With
End With
On Error GoTo 0
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Style = wdStyleHeading1
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
  End With
  Do While .Find.Execute
    Set Rng = .Characters.Last
    With Rng
      .Collapse wdCollapseEnd
      .Style = "Head1Next"
      Do While .ComputeStatistics(wdStatisticWords) < 4
        .MoveEnd Unit:=wdWord, Count:=1
        With .Words.Last
          .Case = wdUpperCase
          .Start = .Start + 1
          .Style = "SmallCaps"
        End With
      Loop
    End With
    If .End = ActiveDocument.Range.End Then Exit Do
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub

您说“第一段的风格与 Word 'Normal' 风格截然不同”,但您没有说明该风格的名称。因此,我的代码创建并引用该样式 'Head1Next'.

注意 'Do While .ComputeStatistics(wdStatisticWords) < 4' 循环。这是确保包含 4 个实际单词所必需的;否则,该范围内的标点符号可能会导致单词计数丢失。

如果将 'Head1Next' 和 'SmallCaps' 样式添加到文档或其模板,则可以省略 'On Error Resume Next' 和 'On Error GoTo 0' 之间与该样式相关的所有内容。使用 'Head1Next',您还可以添加您希望整个段落使用的任何其他字体特征,如注释掉的字体参考所示。