2017-01-03 7 views
0

私は、現在開かれているドキュメントセクションを1つ1つプロットして、ポートレートかランドスケープかを確認し、マージン(ヘッダーとフッターを含む)を指定されたディメンション(ページによって異なります)オリエンテーション)。連続したセクションの向きを確認する方法は?

各セクションのページの向きを検出して現在の「選択」にするか、カーソルを先頭に置くと、次の行で余白が設定され、次のセクションにスキップできますか?

これは私がこれまでに得たものである:

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 

エラーは、私がオブジェクトとして行方不明ですと言います。 Selectionオブジェクト

  • ScreenUpdatingの
  • 不必要な使用が原因となっているラインをオフ
  • 投入されずにオンにされて

    • 宣言されていない変数:

    +0

    https://msdn.microsoft.com/en-us/library/office/ff821149.aspxあなたが最初にあなたがしようとしたものを私たちに示すべきである – Absinthe

    +0

    。 [尋ねる]を読んだことがありますか? –

    答えて

    0

    あなたのコードは、いくつかの問題を持っていますエラーは次のとおりです。

    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 
    
    関連する問題