循环页面或分页符?

时间:2013-11-13 06:40:30

标签: vba ms-word word-vba

我基本上是在尝试为文档创建累积字数,这些文档会将每页上的字数放入页脚并将其添加到每页的总字数中。经过大量的回顾,我发现Word并没有真正为每个人处理相同的页面,因此没有任何界面可以访问各个页面。

现在我正在尝试使用分页符分隔每个页面,因此页面之间有明确的分隔符,但我仍然无法找到如何遍历这些分页符。有线索吗?

我要发布我的代码,但这只是为了获取字数。没有正确的尝试循环浏览分页符,因为我不知道如何。

Sub getPageWordCount()

Dim iPgNum As Integer
Dim sPgNum As String
Dim ascChar As Integer
Dim rngPage As Range
Dim iBeginPage As Integer
Dim iEndPage As Integer
' Go to start of document and make sure its paginated correctly.
Selection.HomeKey Unit:=wdStory, Extend:=wdMove
ActiveDocument.Repaginate

' Loop through the number of pages in the document.
For iPgNum = 2 To Selection.Information(wdNumberOfPagesInDocument)
sPgNum = CStr(iPgNum)
iBeginPage = Selection.Start
' Go to next page
Selection.GoTo wdGoToPage, wdGoToAbsolute, sPgNum
' and to the last character of the previous page...
Selection.MoveLeft wdCharacter, 1, wdMove
iEndPage = Selection.Start
' Retrieve the character code at insertion point.
Set rngPage = ActiveDocument.Range(iBeginPage, iEndPage)
MsgBox rngPage.ComputeStatistics(wdStatisticWords)
'rngPage.Footers(wdHeaderFooterPrimary).Range.Text = rngPage.ComputeStatistics(wdStatisticWords)
'ActiveDocument.Sections(2).Footers
' Check the character code for hard page break or text.
Next

' ActiveDocument.Sections(2).Footers(wdHeaderFooterPrimary).Range.Text = "bob" 'Testing

End Sub

2 个答案:

答案 0 :(得分:2)

终于搞定了,设法通过它猜测我的方式,从互联网的黑暗角落采取各种各样的位:

Sub getPageWordCount()

    'Replace all page breaks with section breaks
    Dim myrange1 As Range, myrangedup As Range
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        Do While .Execute(findText:="^m", Forward:=True, _
            MatchWildcards:=False, Wrap:=wdFindStop) = True
            Set myrange = Selection.Range
            Set myrangedup = Selection.Range.Duplicate
            myrange.Collapse wdCollapseEnd
            myrange.InsertBreak wdSectionBreakNextPage
            myrangedup.Delete
        Loop
    End With

    'Unlink all footers and insert word count for each section
    Dim sectionCount, sectionNumber, i, sectionWordCount, cumulativeWordCount As Integer
    sectionCount = ActiveDocument.Sections.Count
    For sectionNumber = 1 To sectionCount
        With ActiveDocument.Sections(sectionNumber)
            sectionWordCount = .Range.ComputeStatistics(wdStatisticWords)
            cumulativeWordCount = cumulativeWordCount + sectionWordCount
            With .Footers.Item(1)
                .LinkToPrevious = False
                .Range.Text = "This page's word count: " + CStr(sectionWordCount) + "  |  Cumulative word count: " + CStr(cumulativeWordCount)
                .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
            End With
        End With
    Next

End Sub

现在我刚刚发现,如果我想将这个宏移植到一个加载项以便于非技术用户使用,我必须在Visual Studio中的VB 2010中编写它,其中API是不同的。祝你好运!

答案 1 :(得分:0)

听起来好像你有你需要的东西,但是我正在研究一种替代方案,我也可以发布,因为它不需要你添加分页符或分节符。但是你必须在文档中出现的每个页脚中添加相同的嵌套字段(我在这里没有完成那部分,但它并不是完全无关紧要的,因为每个部分可能有多个部分和多个页脚)。

您需要添加的字段代码(除了“此页面的字数:'文字外)

{ DOCVARIABLE "s{ SECTION }p{ PAGE \*arabic }" }

如上所述,该方法可能会在某些情况下中断,例如:如果有连续的断面。我没有检查过。

Sub createWordCounts()
Dim i As Integer
Dim rng As Word.Range
With ActiveDocument
  For i = 1 To .Range.Information(wdActiveEndPageNumber)
    Set rng = .GoTo(wdGoToPage, wdGoToAbsolute, i).Bookmarks("\page").Range
    .Variables("s" & CStr(rng.Information(wdActiveEndSectionNumber)) & "p" & CStr(rng.Information(wdActiveEndAdjustedPageNumber))).Value = rng.ComputeStatistics(wdStatisticWords)
    Set rng = Nothing
  Next
End With

End Sub