Print all pages that are not envelopes in Word 2003/VBA

G

gravyface

Been looking through the MSDN VBA reference, and I'm not sure how to get this
to work. In pseudo-code, I thought I could do something like:

While(!ActiveDocument.Envelope)
PrintOut
...

or perhaps even iterate through each page, check it's type and if envelope,
discard page number, else add to array or collection of some kind, and then
use that to set the page range to print in the PrintOut method.

Client has tons of documents saved with the last page being the envelope.
Problem is, that's not consistent -- they've been saving the documents
without the envelope attached for the last half year so I can't just print
everything but the last page.

Any ideas?
 
J

Jay Freedman

Been looking through the MSDN VBA reference, and I'm not sure how to get this
to work. In pseudo-code, I thought I could do something like:

While(!ActiveDocument.Envelope)
PrintOut
..

or perhaps even iterate through each page, check it's type and if envelope,
discard page number, else add to array or collection of some kind, and then
use that to set the page range to print in the PrintOut method.

Client has tons of documents saved with the last page being the envelope.
Problem is, that's not consistent -- they've been saving the documents
without the envelope attached for the last half year so I can't just print
everything but the last page.

Any ideas?

Your pseudo-code isn't going to work, because "envelope-ness" isn't a property
of a page. There is an Envelope object that's a property of the document as a
whole, and it exists regardless of whether there's an actual envelope in the
document. The VBA help topic on the Envelope object has this helpful remark:

~~~~~~~~~~
The Envelope object is available regardless of whether an envelope has been
added to the specified document. However, an error occurs if you use one of the
following properties when an envelope hasn't been added to the document:
Address, AddressFromleft, AddressFromTop, FeedSource, ReturnAddress,
ReturnAddressFromLeft, ReturnAddressFromTop, and UpdateDocument.

The following example demonstrates how to use the On Error GoTo statement to
trap the error that occurs if an envelope hasn't been added to the active
document. If, however, an envelope has been added to the document, the recipient
address is displayed.

On Error GoTo ErrorHandler
MsgBox ActiveDocument.Envelope.Address
ErrorHandler:
If Err = 5852 Then MsgBox _
"Envelope is not in the specified document"
~~~~~~~~~~

You can adapt this technique as follows:

Function DocHasEnvelope(doc As Document) As Boolean
Dim testStr As String
On Error Resume Next
testStr = doc.Envelope.Address
DocHasEnvelope = (Err.Number = 0)
End Function

Sub Demo()
Dim result As Boolean
result = DocHasEnvelope(ActiveDocument)
If result Then
MsgBox "This document has an envelope"
Else
MsgBox "There is no envelope here"
End If
End Sub

Replace the MsgBox calls with the actions you want to do when there is or is not
an envelope. If there is an envelope, it will be a separate section, and it will
probably have landscape orientation, unlike the rest of the document. That
should let you locate it, whether it's at the beginning or end of the document.
 
G

gravyface

Jay Freedman said:
Replace the MsgBox calls with the actions you want to do when there is or is not
an envelope. If there is an envelope, it will be a separate section, and it will
probably have landscape orientation, unlike the rest of the document. That
should let you locate it, whether it's at the beginning or end of the document.

Didn't quite work as expected -- no fault of yours -- because the documents
don't actually have "true" envelopes in the document: looks like they just
changed the page setup > custom size to 9.5" inches wide. So what I'm doing
now is interating through the Sections collection and testing for PageWidth
values equal to 684 pts (9.5"). Works.
Now, but from what I've seen, every letter has the envelope as the last
page, so I should be able to safely assume that's always true, and set the
print range from 1 to (pgCount - 1).

Question: did some googling on getting an accurate page count and found this
(http://www.word.mvps.org/FAQs/MacrosVBA/GetNumPages.htm):

Solution #1:

Selection.Information(NumberOfPagesInDocument)

Solution #2:

ActiveDocument.BuiltInDocumentProperties("Number of Pages")

Solution #3:

ActiveDocument.Content.ComputeStatistics(wdStatisticPages)

The third method is the most reliable, but the slowest.

I'm assuming since Sections != pages all the time, I can't just use my
section iteration to also gather a page count, correct? Shame to have to
iterate through all the pages while not using that as my pagecount in the
process; save some cycles...

Anything seemed flawed with my approach that you can think of?

Thanks again for your suggestions.
 
J

Jay Freedman

gravyface said:
Didn't quite work as expected -- no fault of yours -- because the
documents don't actually have "true" envelopes in the document: looks
like they just changed the page setup > custom size to 9.5" inches
wide. So what I'm doing now is interating through the Sections
collection and testing for PageWidth values equal to 684 pts (9.5").
Works.
Now, but from what I've seen, every letter has the envelope as the
last page, so I should be able to safely assume that's always true,
and set the print range from 1 to (pgCount - 1).

Question: did some googling on getting an accurate page count and
found this (http://www.word.mvps.org/FAQs/MacrosVBA/GetNumPages.htm):

Solution #1:

Selection.Information(NumberOfPagesInDocument)

Solution #2:

ActiveDocument.BuiltInDocumentProperties("Number of Pages")

Solution #3:

ActiveDocument.Content.ComputeStatistics(wdStatisticPages)

The third method is the most reliable, but the slowest.

I'm assuming since Sections != pages all the time, I can't just use my
section iteration to also gather a page count, correct? Shame to
have to iterate through all the pages while not using that as my
pagecount in the process; save some cycles...

Anything seemed flawed with my approach that you can think of?

Thanks again for your suggestions.

I'm reminded again that "nothing is foolproof because fools are so
ingenious"! :) I'll guess that your clients have no idea there's an
Envelope feature in Word, so they just did whatever seemed to work.

Two comments:

- When testing the page width, it would be best to look for a width _greater
than_ the "normal" width rather than one that's _equal to_ some specific
value -- just in case one of those ingenious fools set an "envelope" to a
slightly different width than you've come to expect.

- Although you could get the page count of each section from

oSec.Range.ComputeStatistics(wdStatisticPages)

and keep a running count through the iteration, that would actually be
slower (though probably not measurably so) than getting a single count for
the entire document. Sometimes elegant programming actually costs more...

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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