Word VBA遍历具有相似名称的书签

问题描述 投票:0回答:1

我有一个用户窗体,如果需要打印文档,该窗体允许用户在封面后插入故意的空白页。当我只需要在整个文档中插入1或2个空白页时,我可以使其工作正常,但是现在我有了一个新文档,如果将用户窗体组合框更改为“可打印”,则总共需要插入14个空白页格式“

我在当前文档中使用的代码在下面作为参考,但我认为要添加这么多空白页,最好使用循环或查找代替它。

我所有要在其中添加空白页的书签都被命名为带有连续编号的“打印”(即“打印1”,打印2”等),因此我希望能够在文档中搜索包含以下内容的所有书签名称“打印”,但我似乎无法弄清楚!

Dim answer As Integer
Dim BMBreak As Range
Dim BMBreak2 As Range

With ActiveDocument

    'Insert bookmarks applicable to Printable Format
    If CbxPrint.Value = "Printable Format" Then

        answer = MsgBox("You have changed the document to Printable Format." & vbNewLine _
                & "This will add intentionally blank pages throughout the document " & vbNewLine _
                & "Do you wish to continue?", vbOKCancel, "WARNING")

        If answer = vbOK Then

            'Intentional blank page after title page
            Set BMRange = ActiveDocument.Bookmarks("Print1").Range

                BMRange.Collapse wdCollapseStart
                BMRange.InsertBreak wdPageBreak
                BMRange.Text = "THIS PAGE IS INTENTIONALLY BLANK"
                BMRange.ParagraphFormat.SpaceBefore = 36
                BMRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
                ActiveDocument.Bookmarks.Add "Print1", BMRange

                With BMRange
                    .Collapse Direction:=wdCollapseEnd
                    .InsertBreak Type:=wdSectionBreakContinuous
                End With

                With ActiveDocument.Sections(3)
                    .Headers(wdHeaderFooterPrimary).LinkToPrevious = False
                    .Footers(wdHeaderFooterPrimary).LinkToPrevious = False
                End With

                With ActiveDocument.Sections(2)
                    .Headers(wdHeaderFooterPrimary).LinkToPrevious = False
                    .Headers(wdHeaderFooterPrimary).Range.Delete
                    .Footers(wdHeaderFooterPrimary).LinkToPrevious = False
                    .Footers(wdHeaderFooterPrimary).Range.Delete
                End With ```


ms-word word-vba
1个回答
0
投票

如下所示的代码将处理任意数量的Print#书签(目前限制为20,不一定全部存在):

Dim i As Long, BMRange As Range
With ActiveDocument
  If CbxPrint.Value = "Printable Format" Then
    If MsgBox("You have changed the document to Printable Format." & vbCr & _
      "This will add intentionally blank pages throughout the document " & vbCr _
      & "Do you wish to continue?", vbOKCancel, "WARNING") = vbOK Then
    'Process bookmarks applicable to Printable Format
      For i = 20 To 1 Step -1
        If .Bookmarks.Exists("Print" & i) = True Then
          'Intentional blank page
          Set BMRange = .Bookmarks("Print" & i).Range
          With BMRange
            .Collapse wdCollapseEnd
            .InsertBreak Type:=wdSectionBreakNextPage
            .InsertBreak Type:=wdSectionBreakNextPage
            .Start = .Start - 1
            .Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
            .Sections.Last.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
            With .Sections.First
              .Headers(wdHeaderFooterPrimary).LinkToPrevious = False
              .Footers(wdHeaderFooterPrimary).LinkToPrevious = False
              .Headers(wdHeaderFooterPrimary).Range.Delete
              .Footers(wdHeaderFooterPrimary).Range.Delete
              .Range.InsertBefore "THIS PAGE IS INTENTIONALLY BLANK"
              .Range.ParagraphFormat.SpaceBefore = 36
              .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
            End With
            .Start = .Start - 1
            .Bookmarks.Add "Print" & i, .Duplicate
          End With
        End If
      Next
    End If
  End If
End With
© www.soinside.com 2019 - 2024. All rights reserved.