How to hide a Print commandbutton on a document form when a user clicks on it

Article contributed by Ibby

When creating a Document/Template Form, you may want to include a CommandButton on the document to print the form. But how do you hide the CommandButton so that it doesn't get printed ? The following will show you one way of achieving this:

1.

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

2.

Insert a CommandButton in the document from the Control Toolbox toolbar.

3.

Right-click on the CommandButton and choose Properties. Change the Captionto Print Document.

4.

To get the CommandButton into the Textbox, you need to Cut the CommandButton, then place the cursor in the Textbox and Pasteit into the Textbox.

5.

Make sure you are in Design Mode – the Design Mode button on the top-left of the Control Toolbox toolbar needs to be depressed.

6.

Now, double-click on the CommandButton – this will take you into the VBE (Visual Basic Editor) and create a CommandButton_Click event procedure. Paste the following code into this procedure:

Private Sub CommandButton1_Click()

With ActiveDocument
    .Shapes(1).Visible = msoFalse
    .PrintOut Background:=False
    .Shapes(1).Visible = msoTrue
End With

End Sub
 

7.

Close the VBE, exit out of Design Mode and the CommandButton should be ready to use.

  

Sample template attached:

Click here to download
  

Alternative solution

Alternatively, the user could simply click the Print button on the Standard toolbar, and if you want anything non-standard to happen during the print job, you could intercept the FilePrintDefault and FilePrint commands, or the DocumentBeforePrint event. See Intercepting events like Save and Print for more details.


Click to view Terms of Use page

Click to view Disclaimer page