VBA跳过For循环 - 为什么?

时间:2013-02-20 10:48:11

标签: vba loops for-loop

我在MS Word Visual Basic编辑器中有这个VBA代码;

这是为了重置页码,使它们连续工作。但是,它似乎跳过循环的全部内容而不执行此操作。

Sub Macro3()
'
' Macro3 Macro
' Test 3
'
Dim GetNumberOfPages

    For IncVar = 1 To GetNumberOfPages
        WordBasic.ViewFooterOnly
        ActiveDocument.AttachedTemplate.BuildingBlockEntries(" Blank").Insert _
            Where:=Selection.Range, RichText:=True
        WordBasic.ViewFooterOnly
        ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 3"). _
            Insert Where:=Selection.Range, RichText:=True
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        Selection.WholeStory
        With Selection.Sections(IncVar).Headers(IncVar).PageNumbers
            .NumberStyle = wdPageNumberStyleArabic
            .HeadingLevelForChapter = 0
            .IncludeChapterNumber = False
            .ChapterPageSeparator = wdSeparatorHyphen
            .RestartNumberingAtSection = False
            .StartingNumber = 0
        End With
        Selection.WholeStory
        Selection.EscapeKey
        ActiveWindow.ActivePane.View.ShowAll = Not ActiveWindow.ActivePane.View. _
            ShowAll
        Selection.EscapeKey
        Selection.EscapeKey
    Next IncVar
End Sub

这是为什么? 我该如何解决?

谢谢,

巴里史密斯

3 个答案:

答案 0 :(得分:5)

如果您使用f8Step Into...您的序列并检查GetNumberOfPages的值,您会看到GetNumberOfPages = Empty和整个循环被跳过

答案 1 :(得分:3)

GetNumberOfPages是一个变量,默认情况下为空。

首先需要为其指定一些值,例如

Dim numberOfPages as Integer
Dim currentPage as Integer
numberOfPages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
For currentPage = 1 To numberOfPages
    ...
Next currentPage

答案 2 :(得分:1)

我认为你是这样想的

Dim GetNumberOfPages as integer = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) //check the syntax . i'm not sure. 

但忘记初始化GetNumberOfPages

相关问题