Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Mar 2004
    Location
    Portland, Oregon
    Posts
    173
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Printing Issue (200 & XP)

    I have a complicated legal document. It is usually about 12-15 pages when it's complete. What I want to do is write a VBA macro that will print, say, pages 1-4, then print 5-6, then print 7-15. I'm having trouble getting Word to recoginze the page numbers. After some testing, I've narrowed it down to the Section Breaks. I removed all the section breaks and Word obeyed just fine. With the section breaks Word either prints the whole document or just the first few pages. I have to keep my section breaks or my footers get all messed up. My question is... Can I use VBA code to print Sections instead of pages?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing Issue (200 & XP)

    You can specify a section to be printed as follows:

    Application.PrintOut Range:=wdPrintRangeOfPages, Pages:="s3"

    You can also specify a range of pages within one section:

    Application.PrintOut Range:=wdPrintRangeOfPages, Pages:="p2s3-p6s3"

    Or a range of pages across several sections:

    Application.PrintOut Range:=wdPrintRangeOfPages, Pages:="p1s3-p4s5"

    The online help for Print (in Word itself, not in Visual Basic) has more information.

  3. #3
    2 Star Lounger
    Join Date
    Mar 2004
    Location
    Portland, Oregon
    Posts
    173
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing Issue (200 & XP)

    Thank you, I'll give that a shot....

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Printing Issue (200 & XP)

    Another approach would be to use a print macro that lets you specify the "physical" pages you want to print, and converts them to "logical" page numbers for you. This is a "first draft" of such a macro, should anyone want to develop it further. As noted in the comments, it's not very fault tolerant.
    <pre>Sub PrintPhysicalPages()
    Dim intLastPhysical As Integer, intCount As Integer
    Dim strFirstUser As String, strLastUser As String, strRange As String
    ' Capture physical page number of the last page
    With ActiveDocument
    intLastPhysical = .Range(.Content.End - 1, .Content.End - 1). _
    Information(wdActiveEndPageNumber)
    End With
    ' Ask user for page range (UserForm would be nicer...)
    ' quit if user leaves either the start or end blank
    strFirstUser = Trim(InputBox("Print from what physical page?", _
    "Choose Start Page", "1"))
    If strFirstUser = vbNullString Then Exit Sub
    strLastUser = Trim(InputBox("Print to what physical page?", _
    "Choose End Page", CStr(intLastPhysical)))
    If strLastUser = vbNullString Then Exit Sub
    ' Calculate the p1s2 style page numbers by scrolling thru
    ' (if user entered invalid data, this will bomb!)
    ' First, the starting page...
    If strFirstUser = "1" Then
    strRange = "p1s1-"
    Else
    ' Start from the top (should first make sure we're in the
    ' main document, but...
    Selection.HomeKey wdStory
    ' Scroll by page count
    With Application.Browser
    .Target = wdBrowsePage
    For intCount = 1 To CInt(strFirstUser) - 1
    .Next
    Next
    End With
    With Selection
    strRange = "p" & .Information(wdActiveEndAdjustedPageNumber) & _
    "s" & .Information(wdActiveEndSectionNumber) & "-"
    End With
    End If
    ' Now calculate the ending page...
    If CInt(strLastUser) = intLastPhysical Then
    Selection.EndKey wdStory
    Else
    ' Start from the top (should first make sure we're in the
    ' main document, but...
    Selection.HomeKey wdStory
    ' Scroll by page count
    With Application.Browser
    .Target = wdBrowsePage
    For intCount = 1 To CInt(strLastUser) - 1
    .Next
    Next
    End With
    End If
    With Selection
    strRange = strRange & _
    "p" & .Information(wdActiveEndAdjustedPageNumber) & _
    "s" & .Information(wdActiveEndSectionNumber)
    End With
    ' Display print dialog pre-loaded with page range
    With Dialogs(wdDialogFilePrint)
    .Range = 4 ' print a range of pages
    .Pages = strRange ' pre-load the box (tested in Word 2002)
    .Show
    End With
    End Sub</pre>

    An importable copy is attached. Hope this helps somebody. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Attached Files Attached Files

  5. #5
    2 Star Lounger
    Join Date
    Mar 2004
    Location
    Portland, Oregon
    Posts
    173
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing Issue (200 & XP)

    Looks really good! Thank you...

Posting Permissions

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