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 に答える