Printing selected sections

T

Tobias Schröer

Hi,

I have Word 2003 document, separated in three sections. The first
section is a kind of header with some form fields (e.g. adress) in a
table. The second and third sections are normal text sections. I have
enable document protection: first section allows form field input,
second and third section are not protected.

Now, I want to print that document, but only the first and second
section. The third section contains some intermal remarks that should
not be published. I tried that by VBA with this statement:

Application.PrintOut _
0, 0, WdPrintOutRange.wdPrintRangeOfPages, psFile, , , , , _
"s1-s1", wdPrintAllPages, True

(I also tried the version with naming the parameters as 'Pages:="s1-s2"')

I tried that also with the word printing dialog and specified "s1-s2" as
printing range.

In either case the whole document is printed. I also tried several
printers: all with the same result. Switching off the document
protection doesen't help either.

Any ideas? Or am I missing something fundamental here?

Thanks
Tobi
 
G

Graham Mayor

The following works for me

Application.PrintOut FileName:="", _
Range:=wdPrintRangeOfPages, Pages:="s1-s2"


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tobias Schröer

Graham said:
The following works for me

Application.PrintOut FileName:="", _
Range:=wdPrintRangeOfPages, Pages:="s1-s2"
I thought it will work for me, too. But it don't :(
I tried all parameter combinations that seems obvious to me, but no success.
What surprised me, is the fact that the Word printer dialog doe's not
work either.

What I have forgot to mention is, that the documnt is based on a
template and the section break are continuous. But I don't think, this
will matter, does it?

Tobi
 
G

Graham Mayor

OK the continuous bit screws things up - but the following should overcome
that

ActiveDocument.Sections(1).Range.Select
Application.PrintOut FileName:="", Range:=wdPrintSelection
ActiveDocument.Sections(2).Range.Select
Application.PrintOut FileName:="", Range:=wdPrintSelection

This will print out the 2 sections on separate pages.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tobias Schröer

Graham said:
OK the continuous bit screws things up - but the following should overcome
that

ActiveDocument.Sections(1).Range.Select
Application.PrintOut FileName:="", Range:=wdPrintSelection
ActiveDocument.Sections(2).Range.Select
Application.PrintOut FileName:="", Range:=wdPrintSelection

This will print out the 2 sections on separate pages.
Thanks,

but I need the printout on a single page / continuously.

I made the break between the sections 2 and 3 a "new page" break and
voilà, it works :)
Not quite what I wanted, but good enough, though.

However, I would suppose Word to be able to handle a printing of
sections with continuous breaks.

Tobi
 
G

Graham Mayor

I'm sure there must be away to select two sections directly, though I can't
think of it at the moment, however

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "*^m*^m"
.MatchWildcards = True
End With
Selection.Find.Execute
Application.PrintOut FileName:="", Range:=wdPrintSelection
Selection.HomeKey Unit:=wdStory

will effectively do that and print the selected text.
If you print continuous sections it appears to print everything on the
page(s) that hold the required sections. Printing selected text on the other
hand does just that.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jonathan West

Hi Tobias

How are your pages numbered? If they are numbered consecutively and
numbering doesn't restart at a new section, then you can use page numbers
instead of section numbers, which should get you past this continuous
section break problem. All you need do is work out the start and end pages
for the sections you want to print, and feed that into the Pages parameter.

If those page numbers vary from document to document, then you can use the
Information property to find the page number of any point in the document -
use the wdInformationActiveEndPageNumber parameter of the property to
retrieve the page number.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
T

Tobias Schröer

Hi

Jonathan said:
Hi Tobias

How are your pages numbered? If they are numbered consecutively and
numbering doesn't restart at a new section, then you can use page
numbers instead of section numbers, which should get you past this
continuous section break problem. All you need do is work out the start
and end pages for the sections you want to print, and feed that into the
Pages parameter.

I think that won't work, if the section break is in the middle of the
last page to print. I would have half a page with section 3 content.
If those page numbers vary from document to document, then you can use
the Information property to find the page number of any point in the
document - use the wdInformationActiveEndPageNumber parameter of the
property to retrieve the page number.

Tobi
 
S

Stefan Blom

in message
I'm sure there must be away to select two sections directly,

The following seems to work:

Dim r As Range
Dim RangeStart As Long
Dim RangeEnd As Long

RangeStart = ActiveDocument.Sections(1).Start
RangeEnd = ActiveDocument.Sections(2).End

Set r = ActiveDocument.Range(RangeStart,RangeEnd)

r.Select

ActiveDocument.PrintOut Range:=wdPrintSelection
 
J

Jonathan West

Tobias Schröer said:
Hi



I think that won't work, if the section break is in the middle of the last
page to print. I would have half a page with section 3 content.

If the section break is halfway down the page, do you want only the first
half of that page to print?

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Graham Mayor

Thanks for that ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Actually that doesn't work for me - I needed the following

RangeStart = ActiveDocument.Sections(1).Range.Start
RangeEnd = ActiveDocument.Sections(2).Range.End

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

He just wants the bits actually contained in the selected sections to print
on the same page. The code I posted earlier does that - somewhat improved by
Stefan's contribution - and tweaked again in my last post.

The problem is that if you print a section with a continuous break from the
dialog, the whole page containing that section prints. This is probably a
bug. Printing selected text does however work correctly.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tobias Schröer

Jonathan said:
If the section break is halfway down the page, do you want only the
first half of that page to print?
Yes. As said before, I want to print the sections 1 and 2, but nothing
of section 3.

Tobi
 
J

Jonathan West

Graham Mayor said:
He just wants the bits actually contained in the selected sections to
print on the same page. The code I posted earlier does that - somewhat
improved by Stefan's contribution - and tweaked again in my last post.

The problem is that if you print a section with a continuous break from
the dialog, the whole page containing that section prints. This is
probably a bug. Printing selected text does however work correctly.

I see, though I think it won't print any headers or footers that may be
present.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

Stefan Blom

Apparently I pasted my first attempt, which didn't work, into the
newsgroup message. Sorry about that!

--
Stefan Blom
Microsoft Word MVP


in message
 

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