首页 > 解决方案 > MS Word vba 重新排列页面顺序

问题描述

如何反转所有页面顺序,因此第 1、2、3、4、5 页变为 5、4、3、2、1,因为这在打印时执行 Option.Reverse。

作为

Sub test()
Dim WA As Object, WD As Object
Dim rngDoc As Word.Range, rngInsert As Word.Range
    Set WA = CreateObject("Word.Application")
    Set WD = WA.Documents.Add(TemplatesName)
    Set rngDoc = WD.Content
    Set rngInsert = rngDoc.Duplicate
        For i = 1 To 100
            With rngInsert
                .InsertBreak Type:=wdSectionBreakNextPage
                .MoveEnd Unit:=wdCharacter, Count:=-1 ' this one reversing pages
                .InsertFile TemplatesName
            End With
        Next i
    WD.SaveAs PdfFile, 17
    WD.Close False: Set WD = Nothing
    WA.Quit False: Set WA = Nothing
End Sub

以相反的顺序插入页面,我需要把它们倒过来

Option.reverse 与

WA.PrintOut OutputFileName:=PdfFile, Range:=wdPrintAllDocument, Item:= _
            wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
            wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=True, _
            PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
            PrintZoomPaperHeight:=0

做我需要的,但这里会警告错误的margings,所以如果我将displayalert设置为False,那么文档不会保存。

标签: vbams-word

解决方案


如果这是您所追求的,您可以通过以下方式反转打印顺序:

Options.PrintReverse = True

但是,如果问题是您以错误的顺序插入页面,则需要如下代码:

Sub Demo()
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim wdRng As Word.Range, i As Long
Const TemplatesName As String = "Template Path & name"
Set wdDoc = wdApp.Documents.Add(TemplatesName)
Set wdRng = wdDoc.Range
For i = 1 To 10
  With wdRng
    .InsertAfter vbCr
    .Start = wdDoc.Range.End
    .InsertBreak Type:=wdSectionBreakNextPage
    .Collapse wdCollapseEnd
    .InsertFile TemplatesName
  End With
Next i
End Sub

推荐阅读