Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing across word sections

Status
Not open for further replies.

Zenkai

Programmer
Dec 17, 2002
31
US
It would be super if we could use absolute page numbers to print pages from a word document. For example, we have two sections, each with 5 pages. Is there a way to have the user input "1-8" and get vb to print out the pages properly? I do NOT want to use the p1s2 stuff.

-Nick
 
Hi Zenkai,

When I input 1-8 in the Print Dialog I get the first 8 pages (5 from the first Section and 3 from the second). I'm afraid I don't understand what your problem is or why you need a code solution. Could you give a bit more detail please?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Well, if the section numbers aren't paged continuously (I.E. the sixth page is numbered as "section 2, page 1") then the regular print method does not work. In that case, you must specify p1s2 in the print area to get it to print that sixth page.
 
Hi Zenkai,

I see the problem. Here is some code which you can run instead of the normal print which takes absolute page numbers and replaces them with p1s1 etc. before invoking the Print.

I can't get the error handling to work properly, so if you enter rubbish (say "abc") you may have to enter the correction twice - try it and I hope you'll see what I mean.

I have not exhaustivly tested this, but it appears to work for me. I'm out most of next week so, if you're totally stuck I hope someone else will jump in to help.

Code:
[blue]Sub PrintSpecial()
    
    Dim rc As Long
    
    With Application.Dialogs(wdDialogFilePrint)

        On Error Resume Next
        Do
            Err.Number = 0
            rc = .Display
            If Err.Number <> 0 Then
                MsgBox "You had an error:  """ & Err.Description & """" & vbNewLine & _
                       "Unfortunately Word has not given me the correction you made." & _
                       vbNewLine & vbNewLine & _
                       "Would you be so kind as to re-enter your requirements?"
            End If
        Loop Until Err.Number = 0
        On Error GoTo 0
        
        If rc = -1 Then  ' OK
            .Pages = ReBuild(.Pages)
            .Execute
        End If
        
    End With

End Sub

Function ReBuild(strPages As String) As String

    Dim vPageRanges As Variant
    Dim vPageNumbers As Variant
    Dim iPageNumber As Integer
    
    Dim i As Integer, j As Integer
    Dim rng As Range
    [green]
    ' N.B.  Input IS syntactically valid; the Dialog doesn't cede control until it is.
    '       Input is not necessarily 'sensible' but the Print itself deals with that.
    [/green]
    vPageRanges = Split(strPages, ",")
    
    For i = 0 To UBound(vPageRanges)
    
        vPageNumbers = Split(vPageRanges(i), "-")
        
        For j = 0 To UBound(vPageNumbers)
            If vPageNumbers(j) <> "" Then
                
                iPageNumber = CInt(vPageNumbers(j))
                
                If iPageNumber <= Selection.Information(wdNumberOfPagesInDocument) Then
                    Set rng = Selection.Range.GoTo(What:=wdGoToAbsolute, Count:=iPageNumber)
                    vPageNumbers(j) = "p" & rng.Information(wdActiveEndAdjustedPageNumber) & _
                                      "s" & rng.Information(wdActiveEndSectionNumber)
                End If
            
            End If
        Next

        vPageRanges(i) = Join(vPageNumbers, "-")
        
    Next
    
    ReBuild = Join(vPageRanges, ",")
    
End Function[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top