2013-02-20 277 views
1

我在MS Word的Visual Basic編輯器中有這個VBA代碼;VBA跳過For循環 - 爲什麼?

這意味着重置頁碼,以便它們連續工作。但是,它似乎跳過循環的全部內容而不執行此操作。

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 

這是爲什麼? 我該如何解決它?

感謝,

巴里·史密斯

+5

'GetNumberOfPages'未分配 – 2013-02-20 10:49:46

+0

請用正確的數據類型定義GetNumberOfPages和循環 – 2013-02-20 10:55:50

回答

5

如果使用f8Step Into...您的序列,並檢查GetNumberOfPages的價值,你會看到GetNumberOfPages = Empty和整個循環被跳過

+0

感謝之前分配一個值!這真的很有幫助......但是我怎樣才能製作一個腳本來返回文檔中的頁面數呢? – 2013-02-20 10:58:20

+1

如果你知道你有10頁,你可以簡單地使用'GetNumberOfPages = 10'。否則,你需要調用一些方法,返回頁面上列出的頁面數量http://support.microsoft.com/kb/185509 – JustinJDavies 2013-02-20 11:03:13

+1

順便說一句。變量GetNumberOfPages的值不是False而是Empty(它是Variant類型的)。 – dee 2013-02-20 11:37:17

1

我認爲你是以這種方式思考

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