Print Specific Pages

J

Jules

We have all of our printers set up to print to duplex printing. I am trying
to set up a macro so that when a user wants to print a current page, it will
print the current page and next page if the current page is an odd number or
the previous page and the current page if the current page is an even number.
I have set up the following, but get a "type mismatch" error when it gets to
the actual "printout".

Private Sub OurPrintOut()
Dim IngPN As Long
Dim IngCheck As Long
Dim FollowingPage As Long
Dim PreviousPage As Long
Dim ThisPage As String
Dim TotalPages As Long
ActiveDocument.Bookmarks.Add Range:=Selection.Range, name:="BackHere"
Selection.EndKey unit:=wdStory
TotalPages = Selection.Information(wdActiveEndPageNumber)
ActiveDocument.Bookmarks("BackHere").Select
IngPN = Selection.Information(wdActiveEndPageNumber)
IngCheck = IngPN Mod 2
If IngCheck > 0 Then
ThisPage = "odd"
ElseIf IngCheck <= 0 Then
ThisPage = "even"
End If
FollowingPage = IngPN + 1
PreviousPage = IngPN – 1
ActiveDocument.Bookmarks("BackHere").Select
If TotalPages > 1 Then
If ThisPage = "odd" Then
ActiveDocument.PrintOut Range:=wdPrintFromTo, From:=IngPN, To:=FollowingPage
ElseIf ThisPage = "even" Then
ActiveDocument.PrintOut Range:=wdPrintFromTo, From:=PreviousPage, To:=IngPN
End If
ElseIf TotalPages < 2 Then
ActiveDocument.PrintOut
End If
End Sub

I have checked that the result of all the definitions is correct, by this I
mean that if I am on page 4 of a 7 page document:
IngPN returns " 4"
FollowingPage returns " 5"
PreviousPage returns " 3"
ThisPage returns "even"
TotalPages returns " 7"
I don't know if the space before each number is significant
Could someone please help me with this
thanks
 
L

Lene Fredborg

I tried to run your macro and got the same error. I found that if I changed
the types of the 3 variables used with PrintOut from _Long_ to _String_, the
macro works and prints the correct pages:

Dim IngPN As String
Dim FollowingPage As String
Dim PreviousPage As String

To find the total number of pages, I think you could use:

TotalPages = ActiveDocument.ComputeStatistics _
(Statistic:=wdStatisticPages, _
IncludeFootnotesAndEndnotes:=True)

This way, you do not need your bookmark and selection change.

If the selection is on the last page and on an odd page, your macro will
attempt to print the next, non-existing page too - but this does not seem to
cause an error. However, you could add a check for IngPN = TotalPages and in
that case print only IngPN.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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