我必须实现的目标:
我的Word文档中有两种类型的数据,“摘要”和“版税”,可以在一个文档中具有这两个部分的多个。但始终按相同的顺序“摘要->版税->摘要->版税”
到目前为止编写的代码如下:
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
我当前的代码如下:它检查版税部分的开头,并获取出现在文档最后的版税部分的结尾,这就是问题,我想获得当前的版税部分。请帮忙。
答案 0 :(得分: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