Print a specific part of document

B

BruceM

Is there a way to print just a selected portion of a document automatically?
I realize the user can select text and print the selection, but the problem
is that the user may not know what exactly to print. Instead, I was
wondering if a macro (or some other automation) could be used to print just
the portion of the document between two bookmarks, or something like that.
If so, how could I make the macro available to other users who open the
file? Only two or three users will need the macro, so adding it to their
Word setups (and setting up a toolbar button to run it, or something of the
sort) individually is not a big deal if it comes to that.
 
F

fumei via OfficeKB.com

"could be used to print just the portion of the document between two
bookmarks"

Yes, absolutely. Say you have two bookmarks, "Yabba", and "DaBaDoo":

Sub PrintBetweenBookmarks(strBM1 As String, strBM2 As String)
Dim r As Range
Set r = ActiveDocument.Range( _
Start:=ActiveDocument.Bookmarks(strBM1).End, _
End:=ActiveDocument.Bookmarks(strBM2).Start)
ActiveDocument.PrintOut Range:=r
Set r = Nothing
End Sub


Sub DoIt()
Call PrintBetweenBookmarks("Yabba", "DaBaDoo")
End Sub

will print from the END of Yabba, to the START of DaBaDoo.
 
F

fumei via OfficeKB.com

BruceM wrote:

If so, how could I make the macro available to other users who open the file?


If the macro is in the document, then the procedure can be executed.
 
B

BruceM

I have been trying to get the macro to work, but I get Error 13 (type
mismatch) on the line:
ActiveDocument.PrintOut Range:=r

I set a watch, and it seems the range is being defined correctly (although I
am not very familiar with VBA for Word), but that line of code has me
stumped. I tried to do some research, but there is no documentation in Help
for the PrintOut method, nor for ActiveDocument or Range. The MS web site
is similarly unenlightening.

Assuming I can resolve the type mismatch, can I put this code into an Add
In, and set up the Word installation from which this will be printed with
that Add In, rather than adding the macro individually to several hundred
documents? I realize there is some work to do adding bookmarks to all of
the documents, but so much the better if I don't need to add the macro over
and over too.
 
T

Tony Jollans

You can't do that.

You can print the Selection - or ranges (in the English sense of the word)
of Pages or Sections.

If you really want to print a Range (in the Word VBA sense of the word) -
which will be repaginated to start at the top of a page - then you must
Select it first and then print the Selection.

--
Enjoy,
Tony

BruceM said:
I have been trying to get the macro to work, but I get Error 13 (type
mismatch) on the line:
ActiveDocument.PrintOut Range:=r

I set a watch, and it seems the range is being defined correctly (although
I am not very familiar with VBA for Word), but that line of code has me
stumped. I tried to do some research, but there is no documentation in
Help for the PrintOut method, nor for ActiveDocument or Range. The MS web
site is similarly unenlightening.

Assuming I can resolve the type mismatch, can I put this code into an Add
In, and set up the Word installation from which this will be printed with
that Add In, rather than adding the macro individually to several hundred
documents? I realize there is some work to do adding bookmarks to all of
the documents, but so much the better if I don't need to add the macro
over and over too.
 
B

BruceM

I don't know how to select a section or range. That is a big part of the
problem. I always need to select some number of pages, but sometimes it is
all of the pages, sometimes the first one only, sometimes the fifth and
sixth pages, and so forth. If I use sections I expect I would need it to be
the same section number in order to use the same code for all documents, but
I have no control over how the author arranges that.
Also, I don't know understand the distinction between range in the English
sense and range in the VBA sense. I want to put a markers into a document
and have Word print everything between the markers.

Tony Jollans said:
You can't do that.

You can print the Selection - or ranges (in the English sense of the word)
of Pages or Sections.

If you really want to print a Range (in the Word VBA sense of the word) -
which will be repaginated to start at the top of a page - then you must
Select it first and then print the Selection.
 
T

Tony Jollans

Given so many variables, the only sure way is the Selection so, instead of

ActiveDocument.PrintOut Range:=r

try

r.Select
ActiveDocument.PrintOut Range:=wdPrintSelection
 
B

BruceM

Thanks for the reply. I'm leaving for the day, but will try out the
suggestion when I return to the office on Monday.
 
B

BruceM

Thanks, that did the trick. It works as it should, no matter where within
the document the range to be printed is located.

I have a follow-up question. I have a new question, too, but I think I
should put that in a new thread.

I have added the code to an add-in, loaded the add-in into the Startup
folder, and created a custom toolbar with the macro as its only command. I
have used an AutoExec macro in the add-in to make the toolbar visilble at
startup, which is also a good thing, but I wonder if I could improve on
that. I would prefer that the toolbar be available only when printing
documents located within a particular folder. Could I test for that path,
and make the toolbar visible only if the document is located within a
particular folder? If so, could that apply to subfolders within that
folder? If not, could I test for the four folders individually?

If testing for the path can't work, could I accomplish my aims in another
way, such as testing for the presence of a particular bookmark?

Or maybe I could load the add-in selectively?
 
B

BruceM

Another question is about how to include the header and footer information
in what I print out. When I select the range the header and footer are not
included. How can I include those?
 
T

Tony Jollans

What you are doing so far sounds fine.

If your toolbar is in your global template it will show whenever that is
available (i.e always) unless you have code to change things dynamically.
The only way to have code fire automatically is using Events. There are
Application Events that could be used to do this - DocumentChange is
probably the one you want. Come back (or maybe post a new question) if you
want help setting this up.
 
T

Tony Jollans

I don't think there's anything you can do about that. They don't print when
you print the Selection.
 
B

BruceM

Oh well. I expect it will be simple enough to create a section containing
the needed range, and move the header and footer information to the document
itself in that section. Still, I was hoping to avoid the extra work.
 
B

BruceM

The toolbar is in the add-in that contains (thanks to your help and the
other posting in this thread) the code to print the range. The AutoExec
macro is in that add-in.
I know about events in Access, but not in Word. I will do some more
investigating now that I know such things exist. I will be out of town for
the next few days, so I won't start any new threads until I return. If I
can't figure it out I will post again. Thanks again for your help.
 

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