2

我基本上是在尝试为文档创建累积字数,将每页上的字数放入其页脚并将其添加到每页的总字数中。环顾四周后,我发现 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
4

2 回答 2

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

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

于 2013-11-13T10:14:57.523 回答
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
于 2013-11-13T11:35:01.583 回答