2017-01-03 29 views
0

我正在研究一個可以通過當前打開的文檔節逐一繪製的宏,檢查它們是縱向還是橫向,並將其頁邊距(包括頁眉和頁腳)設置爲給定尺寸(根據頁面不同方向)。如何檢查每個連續部分的方向?

如何檢測每個部分的頁面方向並將其設置爲當前的「選擇」或將光標置於其開始處,以便下一行可以設置其邊距並跳至下一部分?

這是我走到這一步:

Sub Margins() 
    Dim nOriginalPagination  As Integer 
    Dim objSection    As Section 
    Dim nPaperSize    As Integer 
    Dim ContinueOn    As Boolean 

    ContinueOn = False 
    nNumSects = ActiveDocument.Sections.Count 

    ActiveWindow.View.Type = wdPageView 

    If ActiveWindow.View.SeekView <> wdSeekMainDocument Then 
     ActiveWindow.View.SeekView = wdSeekMainDocument 
    End If 

    Selection.HomeKey wdStory, wdMove 

    For Each objSection In objDocument.Sections 
     iSecNum = Selection.Information(wdActiveEndSectionNumber) 
     With objSection.PageSetup 
      nPaperSize = PAPERLETTER  
     End With 
    Next 

    For Each objSection In objDocument.Sections 
     iSecNum = Selection.Information(wdActiveEndSectionNumber) 
     With objSection.PageSetup 
      'Set the margins, depending on the page orientation 
      If .Orientation = wdOrientPortrait Then 
       .TopMargin = CentimetersToPoints(2.23) 
       .BottomMargin = CentimetersToPoints(2.21) 
       .LeftMargin = CentimetersToPoints(3.17) 
       .RightMargin = CentimetersToPoints(3.17) 
       .HeaderDistance = CentimetersToPoints(0.96) 
       .FooterDistance = CentimetersToPoints(0.94) 
      Else 
       .TopMargin = CentimetersToPoints(3.17) 
       .BottomMargin = CentimetersToPoints(3.17) 
       .LeftMargin = CentimetersToPoints(2.21) 
       .RightMargin = CentimetersToPoints(2.23) 
       .HeaderDistance = CentimetersToPoints(1.9) 
       .FooterDistance = CentimetersToPoints(1.9) 
      End If 
     End With 

     Application.ScreenUpdating = True 
     Options.Pagination = nOriginalPagination 

     Selection.GoTo what:=wdGoToSection, Which:=wdGoToNext 

    Next 

End Sub 

錯誤說我失蹤的對象。

+0

https://msdn.microsoft.com/en-us/library/office/ff821149.aspx – Absinthe

+0

你應該先告訴我們你嘗試過什麼。你讀過[問]嗎? –

回答

0

你的代碼中有幾個問題:

  • 未聲明的變量
  • Selection對象
  • ScreenUpdating的不必要的使用被打開,而不必被關閉

導致該線錯誤是:

For Each objSection In objDocument.Sections 

這是因爲未聲明的變量objDocument未設置爲任何值。 你需要的代碼如下:

Sub Margins() 
    Dim objDocument as Document 
    Dim objSection As Section 

    Application.ScreenUpdating = False 

    Set objDocument = ActiveDocument 
    For Each objSection In objDocument.Sections 
     With objSection.PageSetup 
      'Set the margins, depending on the page orientation 
      If .Orientation = wdOrientPortrait Then 
       .TopMargin = CentimetersToPoints(2.23) 
       .BottomMargin = CentimetersToPoints(2.21) 
       .LeftMargin = CentimetersToPoints(3.17) 
       .RightMargin = CentimetersToPoints(3.17) 
       .HeaderDistance = CentimetersToPoints(0.96) 
       .FooterDistance = CentimetersToPoints(0.94) 
      Else 
       .TopMargin = CentimetersToPoints(3.17) 
       .BottomMargin = CentimetersToPoints(3.17) 
       .LeftMargin = CentimetersToPoints(2.21) 
       .RightMargin = CentimetersToPoints(2.23) 
       .HeaderDistance = CentimetersToPoints(1.9) 
       .FooterDistance = CentimetersToPoints(1.9) 
      End If 
     End With 
    Next 

    Application.ScreenUpdating = True 

End Sub