PDA

View Full Version : Solved: Print specific pages from ALL sections of a word document



xluser2007
11-17-2008, 06:27 PM
Hi All,

I would like to know how to write a VBA code to print pages 5 and 6 of each section of a word document (the macro should be able to check that each section has pages 5 and 6, if not move to the next section etc).

Could anyone please show me how to write this code?

regards

xluser2007
11-17-2008, 07:20 PM
I tried the following:

Option Explicit

Sub test()

Dim oneSection As Section

For Each oneSection In ActiveDocument.Sections

ActiveDocument.PrintOut PrintToFile:=False, Range:=wdPrintRangeOfPages, Pages:=oneSection.range "5-6"

Next oneSection

End Sub
It highlighted the line as a "Syntax Error":

ActiveDocument.PrintOut PrintToFile:=False, Range:=wdPrintRangeOfPages, Pages:=oneSection.range "5-6"
Could anyone please explain how to modify the above, I can;t find any good resources online to help and VBA help file is not of great use for this relatively simple problem (for a Word newb like me).

Any help is sincerely appreciated.:)

xluser2007
11-17-2008, 07:47 PM
A bit of searching and modifying led to the solution.

Here it is for anyone else to benefit from.

Option Explicit

Sub Print_pg5_pg6_all_sections()

Dim oneSection As Section

For Each oneSection In ActiveDocument.Sections

ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintFromTo, From:="p5s" _
& oneSection.Index, To:="p6s" & oneSection.Index

Next oneSection

End Sub

regards,

NOTE: I am not sure why it doesn't print the relevant pages for the sections in the order that they occur in e.g. Section 1 pg5-pg6, Section 2 pg5-pg6, Section 3 pg5-pg6, Section 4 pg5-pg6,.... If anyone could please clarify how to ensure that the the order of sections when printing is preserved, that would be great.

xluser2007
11-17-2008, 08:22 PM
Tried:

Sub Print_pg5_pg6_all_sections_v2()

Dim lngSectionCounter As Long

For lngSectionCounter = 1 To ActiveDocument.Sections.Count

ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintFromTo, From:="p5s" & lngSectionCounter, To:="p6s" & lngSectionCounter

Next lngSectionCounter

End Sub
This works, but doesn't preserve the order of the specific sections, unsure why. Any ideas (for both codes posted)?

fumei
11-18-2008, 01:35 PM
What order does it print?

xluser2007
11-18-2008, 05:27 PM
Hi Gerry,

Thanks for your interest.

It's a bit strange, for documents with a small number of sections it prints in the correct order e.g. Section1-Section6.

But for documents with 10 sections or more it prints (usually) the last 5 first and the first 5 sections last e.g. Section10-Section6 first and Section5-Section1.

This was my experience yesterday. We do use a shared printer, but I wouldn't think it would upset order of printing for the single job sent to it.

Why do you think this may be the case? Do you think it is just specific to me and some print setting (shouldn't be but there may be some option that I may have selected) without knowing the implications?Is there something weird about the code? Or any error-handling required?

Cheers,