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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print last page(s) of a document 1

Status
Not open for further replies.

portlandtrailblazer

Technical User
Feb 13, 2005
3
0
0
US
I have a multi-page MSWord document. I append a section to the end of the document. I want to PRINT ONLY the last page or pages that this appended section is on.
Application is medical records: add information from most recent patient encounter at the end of the MSWord document for that patient, then print the page(s) of the last entry only for filing in paper chart.

MSWord VisualBasic allows printing the current page or a selection. Current page doesnt work if the appended text spans more than one page. Printing selection doesnt work because it excludes document header/footer, and screws up pagination in the printed document relative to the electronic document.

Any hints?? Thanx
 
portlandtrailblazer,

To print consecutive pages, you can specify the [From] and [To] in the printout() method setting at the same time wdPrintFromTo (=3). Something like this.
Code:
const _wdPrintFromTo=3
wordfile="d:\test\abc.doc"
from_page=3
to_page=5

set oword=createobject("word.application")
on error resume next
set odoc=oword.documents.open(wordfile)
odoc.printout false,,_wdPrintFromTo,,cstr(from_page),cstr(to_page)
odoc.close
if err.number<>0 then
	wscript.echo hex(err.number) & vbcrlf & err.description
	err.clear
end if
on error goto 0
set odoc=nothing
oword.quit
set oword=nothing
If you do not know the page numbers, then you can as well use builtinproperties("Number of pages") to determine the [From] and [To] parameters to make the script more dynamic by the difference of it before and after pasting at the end of doc. The necessary ingredients are all here.

regards - tsuji
 
-Amendment-
builtinproperties() I meant builtin[red]document[/red]properties()

- tsuji


 
Thank you, tsuji. I got it to work. Another question, though: I was trying to make it print using the syntax:

Dim intPages As Integer
Dim intPagesAppended As Integer
intPages = ActiveDocument.BuiltInDocumentProperties (wdPropertyPages)
intPagesAppended = intPages + 10
Application.Printout Range:=wdPrintFromTo, Item:=wdPrintDocumentContent, From:=intPages, To:=intPagesAppended, PageType:=wdPrintAllPages

where I used the BuiltInDocumentProperties as you suggested to determine the number of pages before appending to the document; then I added 10 because that will always be enough to get me to the end of the document. However it would not print with the From:=intPages and To:=intPagesAppended arguments. It seems the arguments for From and To, if they are numbers, must be included in quotes?? Of course if I included quotes around the variable (i.e. "intPages") it would not work. What's the correct syntax here?

When I couldnt get this to work I did get your Cstr syntax to work. I just was interested why I couldnt make it work the other way.

Thanks a lot for your help.

 
portlandtrailblazer,

These two parameters are string, else it results in type mismatch error.

A twist, can do like this.
[tt]
intPages = cstr(ActiveDocument.BuiltInDocumentProperties (wdPropertyPages))
intPagesAppended = cstr(cint(intPages) + 10)
[/tt]
Another morbid twist is.
[tt]
n=10
s=cstr(n)
ss="""" & n & """"
[/tt]
It results in s equal exactly ss in all functionality.

I make it more complicated than it should to give you elements to think about. (Bear in mind you are always coding in vba, not vbs. I take it you know what you do.)

- tsuji
 
Sorry, too morbid indeed. It should be
[tt]
ss="" & n
'or
ss="" & n & "" ' to play around more
[/tt]
- tsuji
 
They call it automatic type coersion or type casting.
- tsuji
 
Another detail. If you stay in vba, and cstr() upfront then dim them as string or variant. If you cstr() when assigning to the named parameters, then dim them as integer as you were doing.
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top