【问题标题】:VBA skips a For loop - why?VBA 跳过 For 循环 - 为什么?
【发布时间】:2013-02-05 08:12:23
【问题描述】:

我在 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

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

谢谢,

巴里·史密斯

【问题讨论】:

  • GetNumberOfPages 未分配
  • 请使用适当的数据类型定义 GetNumberOfPages 并在循环之前分配一个值

标签: vba loops for-loop


【解决方案1】:

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

【讨论】:

  • 谢谢!这真的很有帮助......但是我怎样才能制作一个返回文档中页数的脚本呢?
  • 如果你知道你有十页,你可以简单地使用GetNumberOfPages = 10。否则,您需要调用一些方法来返回页数,如本页所述support.microsoft.com/kb/185509
  • 顺便说一句。变量 GetNumberOfPages 的值不是 False 而是 Empty(它是 Variant 类型)。
【解决方案2】:

我认为你是这样想的

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

但是忘记初始化GetNumberOfPages

【讨论】:

    【解决方案3】:

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

    你首先需要给它赋值,例如

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

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多