16

I am making lots of changes to a Word document using automation, and then running a VBA macro which - among other things - checks that the document is no more than a certain number of pages.

I'm using ActiveDocument.Information(wdNumberOfPagesInDocument) to get the number of pages, but this method is returning an incorrect result. I think this is because Word has not yet updated the pagination of the document to reflect the changes that I've made.

ActiveDocument.ComputeStatistics(wdStatisticPages) also suffers from the same issue.

I've tried sticking in a call to ActiveDocument.Repaginate, but that makes no difference.

I did have some luck with adding a paragraph to the end of the document and then deleting it again - but that hack seems to no longer work (I've recently moved from Word 2003 to Word 2010).

Is there any way I can force Word to actually repaginate, and/or wait until the repagination is complete?

4

8 回答 8

8

我刚刚花了 2 个小时试图解决这个问题,但我还没有在任何论坛上看到这个答案,所以我想我会分享它。

https://msdn.microsoft.com/en-us/vba/word-vba/articles/pages-object-word?f=255&MSPPError=-2147217396

这给了我我的解决方案,并结合文章梳理发现人们引用的大多数解决方案在最新版本的 Word 中不受支持。我不知道它更改了哪个版本,但我的假设是 2013 年及更高版本可以使用此代码来计算页数:

ActiveDocument.ActiveWindow.Panes(1).Pages.Count.

我相信它的工作方式是 ActiveDocument 选择文件,ActiveWindow 确认要使用的文件在当前窗口中(如果文件从视图选项卡在多个窗口中打开),Panes 确定是否有多个窗口/拆分窗格/您希望评估“第一个”窗格的任何其他废话,pages.count 通过计算集合中的项目数来指定要评估的页面对象。

任何知识渊博的人都可以随时纠正我,但这是第一个让我在我尝试过的任何文档上获得正确页数的方法!

我也很抱歉,但我不知道如何将该行格式化为代码块。如果模组想要编辑我的评论来做我的客人。

于 2018-01-30T22:19:01.870 回答
7

尝试(也许之后ActiveDocument.Repaginate

ActiveDocument.BuiltinDocumentProperties(wdPropertyPages)

它导致我的 Word 2010 在状态栏中以“计数单词”状态花费半秒,同时ActiveDocument.ComputeStatistics(wdStatisticPages)立即返回结果。

来源: https: //support.microsoft.com/en-us/kb/185509

于 2015-07-15T08:41:54.160 回答
2

完成所有更改后,您可以OnTime在阅读页面统计信息之前强制稍作延迟。

Application.OnTime When:=Now + TimeValue("00:00:02"), _
        Name:="UpdateStats"

我还将更新此OnTime语句之前的所有字段:

ActiveDocument.Range.Fields.Update
于 2013-06-15T11:19:51.667 回答
0

如果不是主题问题的真正答案,我在下面找到了一个可能的解决方法。昨天,下面的第一ComputeStatistics行返回正确的总共 31 页,但今天它只返回 1。

解决方案是摆脱Content对象并返回正确的页数。

Dim docMultiple As Document
Set docMultiple = ActiveDocument 
lPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)  ' Returns 1
lPageCount = docMultiple.ComputeStatistics(wdStatisticPages)  ' Returns correct count, 31
于 2014-07-17T15:21:32.567 回答
0

我在 Excel 中使用了它,它在大约 20 个文档上可靠地工作,没有一个超过 20 页,但有些文档非常复杂,包含图像和分页符等。

Sub GetlastPageFromInsideExcel()
Set wD = CreateObject("Word.Application")
Set myDoc = wD.Documents.Open("C:\Temp\mydocument.docx")
myDoc.Characters.Last.Select        ' move to end of document
wD.Selection.Collapse               ' collapse selection at end
lastPage = wD.Selection.Information(wdActiveEndPageNumber)
mydoc.close
wd.quit
Set wD = Nothing
End Sub
于 2020-03-23T07:36:53.570 回答
0

我在让“ComputeStatistics”返回正确的页数时遇到的一个问题是,我经常在 Word 中的“Web 布局”视图中工作。每当您启动 Word 时,它都会恢复到上次使用的查看模式。如果 Word 处于“Web 布局”模式,则“ComputeStatistics”为脚本处理的所有文件返回页数“1”页。一旦我专门设置了“打印布局”视图,我就得到了正确的页数。

例如:

$MSWord.activewindow.view.type = 3    # 3 is 'wdPrintView', 6 is 'wdWebView'
$Pages = $mydoc.ComputeStatistics(2)  # 2 is 'wdStatisticPages'
于 2022-02-17T01:48:12.317 回答
0

ActiveDocument.Range.Information(wdNumberOfPagesInDocument)

这对我来说每次都有效。它返回单词中的总物理页数。

于 2017-10-26T04:11:11.550 回答
-1
Dim wordapp As Object
Set wordapp = CreateObject("Word.Application")
Dim doc As Object
Set doc = wordapp.Documents.Open(oFile.Path)

Dim pagesCount As Integer
pagesCount = doc.Content.Information(4) 'wdNumberOfPagesInDocument
doc.Close False
Set doc = Nothing
于 2019-05-30T05:42:39.583 回答