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?
Retrieve page numbers from a Word document
Re: Retrieve page numbers from a Word document
- I wrote a JEE_FileGetDetail function, link provided below, which allows you to get details from Explorer columns for files.
- Another idea is to use MS Word and COM objects.
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
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Retrieve page numbers from a Word document
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.
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: Retrieve page numbers from a Word document
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: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
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)
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
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
Re: Retrieve page numbers from a Word document
Code: Select all
Thank you so much for this info on retrieving the total page count in a word document .jonnycube wrote: ↑29 Jan 2018, 16:56Thank 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.
It was driving me nuts trying to get the correct format for the argument .
- flyingDman
- Posts: 2791
- Joined: 29 Sep 2013, 19:01
Re: Retrieve page numbers from a Word document
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