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:
- Insert a “drawing” Textbox ( Insert | Textbox ) in the document. You may like to set the borders to none.
- Insert a CommandButton in the document from the Control Toolbox toolbar.
- Right-click on the CommandButton and choose
Properties. Change the Captionto Print Document.
- 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.
- Make sure you are in Design Mode – the Design Mode button on the top-left of the Control Toolbox toolbar needs to be depressed.
- 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:
- Close the VBE, exit out of Design Mode and the CommandButton should be ready to use.
Private Sub CommandButton1_Click()
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Background:=False
.Shapes(1).Visible = msoTrue
End With
End Sub
Sample template attached:
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 Printt for more details.