find files & number of pages then print

T

taylorkand

I am new to word vba and need to write a macro that does the
following:

1. Search folder for word files
2. For each file found count the number of pages
3. If the number of pages is odd, print
4. If the number of pages is even, print, but print 2 pages per sheet

I have gotten it to find the files in the folder with the following
code, but I don't know how to do steps 2, 3,4. Please help!

Set fs = Application.FileSearch

With fs
.LookIn = "E:\*****"
.FileType = msoFileTypeWordDocuments
If .Execute > 0 Then
Let counted = .FoundFiles.Count
Else
MsgBox "There were no files found."
End If
End With
 
G

Graham Mayor

The following should work - try it on a couple of files in a folder before
committing to a large print run ;)

Sub BatchPrint()
On Error GoTo err_FolderContents
Dim FirstLoop As Boolean
Dim DocList As String
Dim DocDir As String
Dim fDialog As FileDialog
Dim intPages As Integer
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select Folder containing the documents to be printed and click
OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
PathToUse = fDialog.SelectedItems.Item(1)
If Right(PathToUse, 1) <> "\" Then PathToUse = PathToUse + "\"
End With

If Documents.Count > 0 Then
Documents.Close savechanges:=wdPromptToSaveChanges
End If
Application.ScreenUpdating = False
FirstLoop = True
DocList = Dir$(DocDir & "*.doc")

Do While DocList <> ""
Documents.Open DocList
intPages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
If intPages Mod 2 = 0 Then
ActiveDocument.PrintOut , PrintZoomColumn:=2, PrintZoomRow:=1
Else
ActiveDocument.PrintOut
End If

ActiveDocument.Close savechanges:=wdDoNotSaveChanges
DocList = Dir$()
FirstLoop = False
Loop

Application.ScreenUpdating = True
Exit Sub
err_FolderContents:
MsgBox Err.Description
Exit Sub
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

taylorkand

Sorry it took so long to respond, boss had more focus on some other
tasks. Thank you so much though, it works perfectly!
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top