Consulting

Results 1 to 6 of 6

Thread: Solved: Print specific pages from ALL sections of a word document

  1. #1
    VBAX Expert
    Joined
    Dec 2007
    Posts
    520
    Location

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

    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

  2. #2
    VBAX Expert
    Joined
    Dec 2007
    Posts
    520
    Location
    I tried the following:

    [vba]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[/vba]
    It highlighted the line as a "Syntax Error":

    [vba]ActiveDocument.PrintOut PrintToFile:=False, Range:=wdPrintRangeOfPages, Pages:=oneSection.range "5-6"[/vba]
    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.

  3. #3
    VBAX Expert
    Joined
    Dec 2007
    Posts
    520
    Location
    A bit of searching and modifying led to the solution.

    Here it is for anyone else to benefit from.

    [vba]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[/vba]

    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.
    Last edited by xluser2007; 11-17-2008 at 08:44 PM.

  4. #4
    VBAX Expert
    Joined
    Dec 2007
    Posts
    520
    Location
    Tried:

    [vba]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[/vba]
    This works, but doesn't preserve the order of the specific sections, unsure why. Any ideas (for both codes posted)?
    Last edited by xluser2007; 11-17-2008 at 08:43 PM.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    What order does it print?

  6. #6
    VBAX Expert
    Joined
    Dec 2007
    Posts
    520
    Location
    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,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •