萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 辦公室軟體 >  >> Word 教學 >> Word 使用技巧

Word VBA 修改頁面設定完整程式碼(邊距、方向、裝訂線、頁首頁尾、紙張大小等)

透過 PageSetup 物件可完整控制 Word 文件頁面配置,以下為常用屬性設定範例(以 C# 風格語法呈現,VBA 移除型別轉換即可):

WApp.ActiveDocument.PageSetup.LineNumbering.Active = 0                    ' 行號
WApp.ActiveDocument.PageSetup.Orientation = wdOrientPortrait              ' 頁面方向:直向
WApp.ActiveDocument.PageSetup.TopMargin = WApp.CentimetersToPoints(2.54)  ' 上邊距
WApp.ActiveDocument.PageSetup.BottomMargin = WApp.CentimetersToPoints(2.54) ' 下邊距
WApp.ActiveDocument.PageSetup.LeftMargin = WApp.CentimetersToPoints(3.17)  ' 左邊距
WApp.ActiveDocument.PageSetup.RightMargin = WApp.CentimetersToPoints(3.17) ' 右邊距
WApp.ActiveDocument.PageSetup.Gutter = WApp.CentimetersToPoints(0)        ' 裝訂線寬度
WApp.ActiveDocument.PageSetup.HeaderDistance = WApp.CentimetersToPoints(1.5) ' 頁首距離
WApp.ActiveDocument.PageSetup.FooterDistance = WApp.CentimetersToPoints(1.75) ' 頁尾距離
WApp.ActiveDocument.PageSetup.PageWidth = WApp.CentimetersToPoints(21)     ' 紙張寬度
WApp.ActiveDocument.PageSetup.PageHeight = WApp.CentimetersToPoints(29.7)  ' 紙張高度
WApp.ActiveDocument.PageSetup.FirstPageTray = wdPrinterDefaultBin         ' 首頁紙匣
WApp.ActiveDocument.PageSetup.OtherPagesTray = wdPrinterDefaultBin        ' 其他頁紙匣
WApp.ActiveDocument.PageSetup.SectionStart = wdSectionNewPage             ' 節起始:新頁
WApp.ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter = 0             ' 奇偶頁不同頁首頁尾
WApp.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = 0          ' 首頁不同頁首頁尾
WApp.ActiveDocument.PageSetup.VerticalAlignment = wdAlignVerticalTop      ' 垂直對齊:靠上
WApp.ActiveDocument.PageSetup.SuppressEndnotes = 0                        ' 不隱藏尾註
WApp.ActiveDocument.PageSetup.MirrorMargins = 0                           ' 不使用內外邊距
WApp.ActiveDocument.PageSetup.TwoPagesOnOne = False                       ' 不合併列印兩頁
WApp.ActiveDocument.PageSetup.BookFoldPrinting = False                    ' 不手動雙面正面
WApp.ActiveDocument.PageSetup.BookFoldRevPrinting = False                 ' 不手動雙面背面
WApp.ActiveDocument.PageSetup.BookFoldPrintingSheets = 1                  ' 列印份數
WApp.ActiveDocument.PageSetup.GutterPos = wdGutterPosLeft                 ' 裝訂線位置:左側
WApp.ActiveDocument.PageSetup.LinesPage = 40                              ' 每頁行數
WApp.ActiveDocument.PageSetup.LayoutMode = wdLayoutModeLineGrid           ' 版式模式:指定行網格

關鍵說明:

  • 所有邊距、距離屬性單位為**點**,需用 CentimetersToPoints 或 InchesToPoints 轉換。
  • Orientation 可設 wdOrientPortrait (直向) 或 wdOrientLandscape (橫向)。
  • SectionStart 控制節分隔類型(新頁、連續、奇數頁、偶數頁)。
  • GutterPos 配合 MirrorMargins 用於雙面裝訂。
  • 此程式碼適用於整份文件或特定節(先選取節再設定)。
copyright © 萬盛學電腦網 all rights reserved