繁体   English   中英

Word VBA 循环浏览类似名称的书签

[英]Word VBA Loop through bookmarks of similar names

我有一个用户表单,允许用户在需要打印文档时在封面后插入一个有意的空白页。 当我只需要在整个文档中插入 1 或 2 个空白页时,我可以让它正常工作,但是我现在有一个新文档,如果用户表单 combobox 更改为“可打印”,我需要在其中插入总共 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 ```


如下代码将处理任意数量的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM