如何检查每个连续部分的方向?

时间:2017-01-03 10:20:32

标签: vba ms-word word-vba

我正在研究一个宏,它将逐个绘制当前打开的文档部分,检查它们是纵向还是横向,并将它们的边距(包括页眉和页脚)设置为给定尺寸(根据不同而不同)页面方向)。

如何检测每个部分的页面方向并使其成为当前的选择"或者把光标放在它的开头,所以下一行可以设置它的边距并跳到下一节?

这是我到目前为止所得到的:

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

错误说我失踪为对象。

1 个答案:

答案 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