彰化一整天的論壇

 找回密碼
 立即註冊
查看: 394|回復: 0

word VBA很多節,若節的頁數是偶數,就加入空白頁

[複製鏈接]
發表於 2016-12-26 16:08:58 | 顯示全部樓層 |閱讀模式
I am new to VBA coding and even newer to these forums.  

I have a Word document (Word 2003) with multiple sections. Each section is multiple pages.

What I would like to do is to write a macro that would do the following. I would like it to work when run from anywhere in that section.

1. move the cursor to the end of the section
2. count the number of pages in the section
3. If the number of pages is even, add a page; if it is odd, do not add a page.
4. add a section break (odd page)
  1. Sub Breaks()
  2.     Dim Sect As Long
  3.     Dim Pgs As Long
  4.     Dim Rng As Range
  5.     With ActiveDocument
  6.          'Get the number of the current section
  7.         Sect = Selection.Range.Sections.Item(1).Index
  8.          ' set Range
  9.         Set Rng = .Sections(Sect).Range
  10.          ' resize to NOT include Section break
  11.         Rng.MoveEnd Unit:=wdCharacter, Count:=-1
  12.          ' get number of pages
  13.         Pgs = Rng.ComputeStatistics(wdStatisticPages)
  14.          'If pages/2 = 0 then even number, so do process
  15.         If Pgs Mod 2 = 0 Then
  16.             With Rng
  17.                  'Go to the end of the section
  18.                 .Collapse (wdCollapseEnd)
  19.                  'Delete the existing section break
  20.                 .SetRange Start:=Rng.End, End:=Rng.End + 1
  21.                 .Delete
  22.                  'insert a page break
  23.                 .InsertBreak Type:=wdPageBreak
  24.                  'Add an OddPage break
  25.                 .InsertBreak Type:=wdSectionBreakOddPage
  26.             End With
  27.         End If
  28.     End With
  29. End Sub
複製代碼


文章來源: http://www.vbaexpress.com/forum/ ... of-pages-in-section
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

 ㄚ母滴雞湯
 員林香純滴雞精

Archiver|手機版|小黑屋|彰化一整天的論壇(Excel,Office)  |网站地图

GMT+8, 2018-11-20 00:06 , Processed in 0.111029 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表