簡體   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