【问题标题】:Insert Blank if Section ends on ODD page Word Macro如果节结束于奇数页 Word 宏,则插入空白
【发布时间】:2019-06-18 11:06:46
【问题描述】:

我必须达到的目标:

我的word文档中有两种数据,“摘要”和“版税”,在一个文档中可以有这两个部分的多个部分。但总是以相同的顺序“摘要->版税->摘要->版税”

  1. 我必须在每个摘要页面之后添加一个空白页面。总是最多一页。
  2. 对于版税,如果版税部分从第 2 页开始并在第 4 页结束,则意味着总页数为 3,在这种情况下,我必须在此之后添加一个空白页。

目前编写的代码如下:

Sub Add_Page_After_Summary()
'
' Add_Page_After_Summary Macro

' Add_Page_After_Summary
'
' Sample Code
ActiveDocument.Range.Select
Do

With Selection.Find
        .Text = "S U M M A R Y             "
        .Execute
End With

    If Selection.Find.Found Then

        Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=1
        Selection.InsertBreak Type:=wdPageBreak

    Else: GoTo nxt

    End If
Loop


nxt:

ActiveDocument.Range.Select

Do

With Selection.Find
        .Text = "R O Y A L T Y             "
        .Execute
End With

    If Selection.Find.Found Then
        Dim startpage As Integer
        Dim endpage As Integer
        startpage = Selection.Information(wdActiveEndPageNumber)
        Selection.GoTo What:=wdGoToBookmark, Name:="\Section"
        endpage = Selection.Information(wdActiveEndPageNumber)

        Dim difference As Integer
        difference = endpage - startpage

        If difference Mod 2 > 0 Then
            Selection.GoTo What:=wdGoToBookmark, Name:="\Section"
            Selection.MoveRight Unit:=wdCharacter, Count:=1
            Selection.MoveLeft Unit:=wdCharacter, Count:=1
            Selection.InsertBreak Type:=wdPageBreak
        End If

    Else: Exit Sub

    End If
Loop

End Sub

我当前的代码如下:它检查版税部分的开始,并获取文档最后出现的版税部分的结尾,即问题,我想获取当前的版税部分。请帮忙。

【问题讨论】:

  • 代码如何识别版税部分的结束位置?
  • 我们将在它旁边有摘要部分。
  • 所以又一次搜索摘要?
  • 我想是的,如果我们找到它的下一个摘要,我猜它会起作用..
  • 我们需要获取即将到来的Summary Page的页码,从中我们可以得到当前Royalty Section的总页数,并可以进行奇数校验

标签: vba ms-word


【解决方案1】:

如果我对您的理解正确,听起来您好像希望每个部分都从奇数页开始。如果这是正确的,那么在每个摘要或版税标题之前添加一个奇数分页符应该可以解决问题。这会强制在文档打印或另存为 pdf 时根据需要添加空白页。

Sub Test()
  InsertSectionBreaks "S U M M A R Y             "
  InsertSectionBreaks "R O Y A L T Y             "
End Sub

Sub InsertSectionBreaks(FindText As String)
  Dim FindRange As Word.Range, SectionRange As Word.Range
  Dim Found As Boolean

  Set FindRange = ActiveDocument.Content

  ' Find next section based on text, insert odd page section break just before
  FindRange.Find.ClearFormatting
  With FindRange.Find
    .Text = FindText
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    Found = .Execute
  End With

  Do While Found
    'avoid adding a section break at beginning of document
    If FindRange.Information(wdActiveEndAdjustedPageNumber) > 1 Then
      Set SectionRange = FindRange.Duplicate
      With SectionRange
        .Expand wdParagraph
        .Collapse wdCollapseStart
        .InsertBreak Type:=wdSectionBreakOddPage
      End With
    End If
    FindRange.Collapse wdCollapseEnd
    Found = FindRange.Find.Execute
  Loop
End Sub

【讨论】:

  • 我在文档的第一行有公司名称以及“摘要”或“版税”。上面的宏代码只是将公司名称放在我们正在添加的空白页面中。我们可以处理吗?
  • 请看这里发布的输出......stackoverflow.com/questions/56734697/…
  • @RamSingh 我已经编辑了答案中的代码以在插入分节符之前扩展范围。以后请确保您的问题包含复制您的问题所需的所有信息。
  • 现在它正在打印上一页的最后一行。 :(
  • @RamSingh 唯一可能的方法是如果“上一页的最后一行”是包含您正在搜索的文本的段落的一部分。由于我们没有您的文件,因此您提供清晰准确的信息至关重要。请仔细分析文档的结构并发布准确重现问题所需的详细信息。
猜你喜欢
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多