Macro to print page 1

M

Mickey

Hi,
I'm short on time and I'm wondering if anyone has a macro that will print
page 1 of every document in a directory.

Thanks,
Mickey
 
Z

zkid

Maybe a place to start:

Dim myFile, strLookin as string 'this is the full directory path

strLookin = "c:\mydirectory"

myFile = Dir$(strLookin)
Do While myFile <> ""
'put in here or call code to retrieve myFile, print out first page and
close file
myFile = Dir$
Loop

Hope it helps.
 
Z

zkid

I forgot to mention that you should place *.doc in your path to make sure
that only Word documents are being retrieved (e.g., "c:\mydirectory\*.doc")
 
M

Mickey

This is what I have so far. It doesn't work. Can you tell me what's wrong.

Thanks so much for all your help.

Dim myFile, strLookin As String 'this is the full directory path

strLookin = "L:\Athens\Department Shared\Documentation\APPROVED\Analytical
Work Sheets\Material\*.doc"

myFile = Dir$(strLookin)
Do While myFile <> ""
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="1", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True,
PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
'put in here or call code to retrieve myFile, print out first page and
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
myFile = Dir$
Loop
 
Z

zkid

My goodness, that is quite the path!

Here's the corrected code:

Dim myFile, strLookin As String 'this is the full directory path

strLookin = "L:\Athens\Department Shared\Documentation\APPROVED\Analytical
Work Sheets\Material\*.doc"

myFile = Dir$(strLookin)
Do While myFile <> ""
Application.PrintOut FileName:=myFile, Range:=wdPrintRangeOfPages,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="p1s1", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
myFile = Dir$
Loop

Here's the main issue:

Application.PrintOut FileName:="" Needs the current filename (namely,
myFile). So, it looks like this: Application.PrintOut FileName:=myFile (no
quotes when it's a variable in place of a string).

Also, for safety's sake, to make sure it prints page 1 of section 1:

Pages:="p1s1"

For real safety's sake, you should use the network's actual directory
instead of a mapped drive. This way, if the mapped drive is changed, it
won't affect the code: (e.g., it will look something like this:
"\\DOCS\VOL1\Athens\Department Shared\Documentation\APPROVED\Analytical
Work Sheets\Material\*.doc"). Of course, you'll need to replace the
\\docs\vol1 with the correct network drive - don't forget to use double
backslashes in the beginning.
 
M

Mickey

Thanks so much for your patience and help. When I run the macro I get the
following error. Run-time error 5121 - The document name or path is not
valid. Check the file permissions for the document or drive or Use the file
open dialog box to locate the document. In VBA the entire Application.Print
code is hi-lighted in yellow. What am I doing wrong now?

Thanks again.
 
Z

zkid

Okay, here's where we need to start. Have you created a test directory on
your hard drive instead of accessing the network? I suggest creating the
following location and copying in one of the Word documents: c:\test

Then, for the strLookin variable do as follows:

strLookin = "c:\test\*.doc"

If that works, then try doing what I suggest using the actual network path
in lieu of the mapped drive designation.

If you still get the error message, then please tell me what version of
Office you are running as well as the operating system.
 
J

Jonathan West

Mickey said:
Hi,
I'm short on time and I'm wondering if anyone has a macro that will print
page 1 of every document in a directory.

Thanks,
Mickey

Take a look here

Find & ReplaceAll on a batch of documents in the same folder
http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

In the code sample in the article, replace the find/replace code in the
middle of the loop with the appropriate PrintOut command
 
M

Mickey

I replaced "Dialogs(wdDialogEditReplace).Show" with "Application.PrintOut
FileName:=myFile, Range:=wdPrintRangeOfPages,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="p1s1", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0". It runs thru all the
files but page 1 DOESN'T print. What am I doing wrong?

Thanks so much for you help.
Mickey
 
J

Jonathan West

Mickey said:
I replaced "Dialogs(wdDialogEditReplace).Show" with "Application.PrintOut
FileName:=myFile, Range:=wdPrintRangeOfPages,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="p1s1", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0". It runs thru all the
files but page 1 DOESN'T print. What am I doing wrong?

Thanks so much for you help.


I'm not sure, but try these possibilities

1. Does MyFile contain the full pathname of the document? Try instead using
ActiveDocument.Printout and omit the FileName paramater

2. Set the Background parameter to False. Word gets terribly confused if it
tries simultaneously to background print a large number of files.

3. Many of the other parameters can be omitted as they are set to their
defaults. Try this

ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, _
Pages:="p1s1", Background:= False
 
M

Mickey

I did some tweaking and got it to semi-work. It loops thru all the documents
in the directory but only prints page 1. It looks like I need to replace
the code "With Dialogs(wdDialogEditReplace) . ReplaceAll=1 .Execute", but I
don't know what to replace it with. Everything I try doesn't work.

Thanks again for the help.
Mickey
 
M

Mickey

I sent you an email earlier that was incorrect. I meant to say "I did some
tweaking and got it to semi-work. It loops thru all the documents
in the directory but only prints page 1 OF THE FIRST DOCUMENT. It looks like
I need to replace
the code "With Dialogs(wdDialogEditReplace) . ReplaceAll=1 .Execute", but I
don't know what to replace it with. Everything I try doesn't work".
By now I'm sure you think I'm crazy.

Sorry and thanks so much.
Mickey
 
G

Graham Mayor

The following macro prints the first page of each document in the folder
selected.

Sub BatchPrintPageOne()
Dim myFile As String
Dim PathToUse As String
Dim MyDoc As Document
Dim iFld As Integer

With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(PathToUse, 1) = Chr(34) Then
PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2)
End If
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set MyDoc = Documents.Open(PathToUse & myFile)

ActiveDocument.PrintOut Pages:="p1s1", Range:=wdPrintRangeOfPages

MyDoc.Close SaveChanges:=wdDoNotSaveChanges
myFile = Dir$()
Wend
End Sub


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Z

zkid

Ugh, you poor guy - we've just put you through the wringer, haven't we?!

I'm am SO sorry. I just took another look at the code I gave you, and I
forgot to manipulate the path. I have completely tested the following code
(other than the fact that I don't have the same network path), and it works
perfectly. Please give it another go, but I do suggest you replace the
strLookin variable with something simple like c:\test and try it out first.

Dim myFile, strLookin As String 'this is the full directory path

strLookin = "L:\Athens\Department Shared\Documentation\APPROVED\Analytical
Work Sheets\Material\"

'Need to tack on doc at the end of the path
myFile = Dir$(strLookin & "*.doc")
Do While myFile <> ""
'The actual doc to retrieve is the full path plus the current file name
Application.PrintOut FileName:=strLookin & myFile,
Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="p1s1", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True, Background:= _
True, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
myFile = Dir$
Loop
 

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