Retrieve page numbers from a Word document

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jonnycube
Posts: 2
Joined: 29 Jan 2018, 13:08

Retrieve page numbers from a Word document

Post by jonnycube » 29 Jan 2018, 14:59

I've always thought this should be a simple problem to solve, but through all my years of using AutoHotKey I haven't found a way to retrieve the number of pages from a Word document. I've done many web searches, and even gone as far as creating my own poor man's OCR script that uses ImageSearch to compare the on-screen page number with bitmaps of each digit to splice together the information.

Is there a simple way to take the file detail circled in my screen shot and assign it to a variable?
Word page numbers.png
Word page numbers.png (35.95 KiB) Viewed 786 times

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Retrieve page numbers from a Word document

Post by jeeswg » 29 Jan 2018, 15:41

- I wrote a JEE_FileGetDetail function, link provided below, which allows you to get details from Explorer columns for files.

Code: Select all

q:: ;file get page count
;[JEE_FileGetDetail function]
;How to get length of an audio file? - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=40028&p=182781#p182781

vPath := "C:\MyFile.docx"
MsgBox, % JEE_FileGetDetail(vPath, "Pages")
return
- Another idea is to use MS Word and COM objects.

Code: Select all

q:: ;word - get page count
vPath := "C:\MyFile.docx"
oWd := ComObjCreate("Word.Application")
vTrue := -1
oWd.Visible := vTrue
oWd.Documents.Open(vPath)
wdStatisticPages := 2
MsgBox, % oWd.ActiveDocument.ComputeStatistics(wdStatisticPages)
oWd := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

jonnycube
Posts: 2
Joined: 29 Jan 2018, 13:08

Re: Retrieve page numbers from a Word document

Post by jonnycube » 29 Jan 2018, 16:56

Thank you, a thousand times. This has been plaguing me for years and it's a relief to find a solution. I confess that your code is way over my head, and I see I was a long way from finding my own solution, but I was able to wrangle it to get the information I need.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Retrieve page numbers from a Word document

Post by FanaticGuru » 29 Jan 2018, 17:08

I have used FGP - FileGetProperties below:
https://autohotkey.com/boards/viewtopic.php?t=3806

If you want to do it directly yourself you can use this:

Code: Select all

sFolder := A_Desktop "\Test"
sFile := "blank.docx"

oShell := ComObjCreate("shell.application")
oFolder := oShell.NameSpace(sFolder)
oFile := oFolder.ParseName(sFile)
MsgBox % oFolder.GetDetailsOf(oFile, 152)
Every property has a number. 152 seems to be pages on my files but I am not sure that is always the property number on all systems and all versions of Word. I tried on doc, docx, and docm files on my computer and it was 152.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

wpslo
Posts: 1
Joined: 01 Feb 2023, 14:55

Re: Retrieve page numbers from a Word document

Post by wpslo » 01 Feb 2023, 15:00

jonnycube wrote:
29 Jan 2018, 16:56
Thank you, a thousand times. This has been plaguing me for years and it's a relief to find a solution. I confess that your code is way over my head, and I see I was a long way from finding my own solution, but I was able to wrangle it to get the information I need.
Thank you so much for this info on retrieving the total page count in a word document .
It was driving me nuts trying to get the correct format for the argument . :D :D

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Retrieve page numbers from a Word document

Post by flyingDman » 01 Feb 2023, 20:18

Another way:

Code: Select all

oWd := ComObjActive("Word.Application")
oWd.ActiveDocument.Repaginate						; <<< important!
msgbox % oWd.ActiveDocument.ActiveWindow.Panes(1).Pages.count
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”