HidingMacroButtonInPrintout

M

mike

I have a word document that has a macro print page button on each page.
The print code looks like
“Private Sub CommandButton5_Click()
ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, _
Pages:="6", _
Copies:=1
End Subâ€
How do you make a macro button embedded in a document not appear in the
document when printed?
If additional code is needed, where would this code be placed in the above
Segment?

Thanks for any help
Mike
 
M

mike

Jay
Not working.
When I paste these directions, first I get the whole document
to print instead of just page 6

And the print command button shows on the page. What position
in the below stated code do I paste the information code that you refered to.

Thanks for responding
 
J

Jay Freedman

I assumed -- which I should never do -- that you had some idea of how
the code that you posted does its work. Sorry... here's the novice
version.

First, you need to know about the Shapes collection. It's a list of
all the non-inline graphics objects in the document. Each of the
command buttons is a Shape object in that collection, and there might
be others (pictures, drawing objects, and text boxes are also Shape
objects). Each Shape object in the collection has a number, called its
index; the first Shape in the collection is referred to as Shapes(1),
the next as Shapes(2), and so on.

If the only Shape objects in the document are the command buttons,
then probably CommandButton1 is Shapes(1) and so on. If there are
other Shape objects, you'll have to experiment a bit to find the right
indexes to point to the command buttons.

The other thing you ran into is that the code in the mvps.org article
just has a PrintOut command with no extra information, and that prints
the entire document. The version in your code, with the Range and
Pages information, limits the printing to one page.

The last bit you need to notice is that there is a separate macro
procedure for each command button, and it contains a different number
in the Pages item of the PrintOut command. That's how each button
"knows" which page it's on. (As an aside, the "Copies:=1" part isn't
necessary because Word will use that value even if you omit it.)

Putting these things together, the code for the first command button
will be -- assuming that CommandButton1 really is Shapes(1) --

Private Sub CommandButton1_Click()
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Range:=wdPrintRangeOfPages, Pages:="1"
.Shapes(1).Visible = msoTrue
End With
End Sub”

and each of the others will be the same except for replacing the
number 1 in four places with the proper number for that command
button.
 
M

mike

Jay
Almost there, I get what you are saying.
Here is what some further info that I hope you can help.
Page 1 has 20 command buttons.
Command button 2 print out page 3. Page 3 has 2 command button
named 12 and 13. How can I modify what you sent so that when command
button 2 is clicked, it will print out page 3 and hide button 12 and 13.

Command buttons 12 and 13, let the operator either print out page 3 or
return to page 1. They are navigational aides in the document.

thanks for your help.
 
J

Jay Freedman

In the sample code I showed (repeated here for reference):

Private Sub CommandButton1_Click()
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Range:=wdPrintRangeOfPages, Pages:="1"
.Shapes(1).Visible = msoTrue
End With
End Sub

The number in the first line in "CommandButton1_Click" determines
which button causes the macro to run. In your situation, you need it
to say "CommandButton2_Click".

The number in the ".Shapes(1).Visible" lines determine which button is
being hidden and unhidden. Since you want to hide/unhide two buttons,
you need two of each line:

.Shapes(12).Visible = msoFalse
.Shapes(13).Visible = msoFalse

before the .PrintOut statement and then

.Shapes(12).Visible = msoTrue
.Shapes(13).Visible = msoTrue

after it.

The number in the .PrintOut line, after Pages:=, determines what page
to print, so you need Pages:="3" there.
 
M

mike

Jay
Thanks for your help, When I used your suggestions in the below
stated lines
private Sub CommandButton2_Click()
..Shapes(12).Visible = msoFalse
..Shapes(13).Visible = msoFalse
ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:="3", Copies:=1
..Shapes(12).Visible = msoTrue
..Shapes(13).Visible = msoTrue
End Sub

I receive a "Invalid or unqualified reference" statement
Again command buttons 12 and 13 are on page 3.
Command button2 is on page 1 which is an index page with many buttons.
Thanks again for your help
 
J

Jay Freedman

Look carefully at what I posted. You left out the "With ActiveDocument" and
"End With" lines, and you left in the unwanted "ActiveDocument" in the
PrintOut line.

The reason for the "With ActiveDocument" line is to provide the "reference"
for each line that starts with a dot. The dot means "member of", as in "the
Shapes collection is a member of the ActiveDocument object". The "With"
statement is a shortcut -- it says "wherever you see a dot without an
object's name in front of it, use the object in this With statement". And
whenever you use a With statement, you must have an End With statement to
match it.

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

mike

Jay, thanks for hanging in there.
Read you response and changed lanquage to:

Private Sub CommandButton2_Click()
With ActiveDocument
..Shapes(12).Visible = msoFalse
..Shapes(13).Visible = msoFalse
..PrintOut Range:=wdPrintRangeOfPages, Pages:="3"
..Shapes(12).Visible = msoTrue
..Shapes(13).Visible = msoTrue
End With
End Sub

Now I receive "the index into the specfic collection is out of bounds"
statement
when I debug.--
Michael
 
J

Jay Freedman

That's telling you that there aren't really 12 or 13 shapes in the
ActiveDocument.Shapes collection. In fact, I suspect that the command buttons
aren't Shapes, but InlineShapes instead. That's a similar collection, but of
shapes that behave like characters instead of floating above or below the text
layer. Try replacing the word 'Shapes' in four places with 'InlineShapes' and
see if that helps.

The other possibility is that the numbers in the names of the command buttons
aren't really their index numbers in the collection. One way to find out is to
open the Immediate window in the VBA editor and enter a command like

ActiveDocument.InlineShapes(12).Select

or

ActiveDocument.Shapes(12).Select

and see which button (if any) becomes selected.
 
M

mike

Jay
When I ran program as stated below
Private Sub CommandButton2_Click()
With ActiveDocument
..InlineShapes(12).Visible = msoFalse
..InlineShapes(13).Visible = msoFalse
..PrintOut Range:=wdPrintRangeOfPages, Pages:="3"
..InlineShapes(12).Visible = msoTrue
..InlineShapes(13).Visible = msoTrue
End With
End Sub

At the first inline shapes statement I received
compile error,Method or data member not found.

When I ran the macro for inlinshape 12 as you suggested, the
program did highlight the shape, figure on page 3 correctly.
I did draw these buttons with the command draw button.

I do appreciate your help in this. If you are out of ideas, thanks
for your time and suggestions.
 
J

Jay Freedman

My apologies. I should have checked this before giving you bad advice.

A Shape object (floating graphic) has a Visible property. For no reason that I
can discover, an InlineShape object does not have a Visible property, and that's
why an error occurs there. Also, there seems to be no way to convert a command
button into a Shape object that obeys the setting of its Visible property.

That's why the first step in the procedure at
http://word.mvps.org/FAQs/TblsFldsFms/HidePrintButton.htm is:

1. Insert a “drawing” Textbox ( Insert | Textbox ) in the document. You may
like to set the borders to none.

The code in the macro changes the visibility of the textbox, and that in turn
controls the visibility of the command button inside it. There is no way to
change the visibility of a "bare" command button.

To get to the right behavior from where you are now, insert textboxes in the
same order as the command buttons-- that is, draw the first textbox, cut
CommandButton1 and paste it into the box; draw the second textbox, cut
CommandButton2 and paste it into that box; and so on. Then the index of each
textbox will match the number of the command button that it contains. Be careful
not to get them out of order.

Then change the macro back to using .Shapes(12) and .Shapes(13), and it should
work.

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

mike

Jay
Thanks for the last bit of information. I know now what I have to
do and why.
Again, thanks for hanging in until I now see how to accomplish what
I intended to do.
 

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