PDA

View Full Version : List word files and their size and pages



entwined
01-17-2012, 09:43 PM
Hello troops, need some help here please. :)

I need a Word macro that will list all the files with their filesizes and pages in a single directory. The directory only contains word files (.doc, .docx) and nothing else.

I have created one (code below but i don't know if I've done it right). The filenames and filesizes are fine but the pages always displays "3" even if the word files' pages aren't really 3 pages.

The value of the input box will be the path. It'll just be pasted there. Please help I really need this. Thanks alot.... :)

============

Sub listpageandsize()

invFiles = InputBox("Input path of the files:", "Input Path")

Selection.HomeKey unit:=wdStory
Selection.TypeText "Filename" & vbTab
Selection.TypeText "Size" & vbTab
Selection.TypeText "No. of Pages" & vbCrLf

On Error Resume Next
Set fso = CreateObject("scripting.filesystemobject")
Set fld = fso.GetFolder(invFiles)
For Each fil In fld.Files
lFname = fil.Name
lSize = fil.Size

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(invFiles & "\" & lFname)

lPages = objDoc.BuiltInDocumentProperties(wdPropertyPages)
objDoc.Save
objWord.Quit

Selection.EndKey unit:=wdStory
Selection.TypeText lFname & vbTab
Selection.TypeText lSize & vbTab
Selection.TypeText lPages & vbCrLf

Next

End Sub

rruckus
01-23-2012, 02:38 PM
Use .ActiveWindow.Panes(1).Pages.Count instead.

lPages = objDoc.ActiveWindow.Panes(1).Pages.Count

entwined
01-23-2012, 06:38 PM
Thanks for the help.

I also found an alternate solution and much quicker to execute :)

Documents.Open FileName:=invFiles & "\" & lFname
lPages = ActiveDocument.ComputeStatistics(wdStatisticPages)
ActiveDocument.Close False