Hey guys welcome back. So in this video we're going to discuss the workbook before print event and the workbook before print event. As the name suggests, it runs any code written in that event before printing happens. So to get to that event, you need to open your VBA editor and then you're going to double click on this workbook here and then you're going to click on workbook from this menu and you're going to select before print here from this menu, I'm just going to delete the workbook open event because we don't need it. So it actually works similarly to the workbook before close event and you can see here there is a parameter called cancel and this cancel parameter is Boolean so it takes values of true or false is by default false. However, if you put it equal to true then printing will not be allowed.
If someone tries to print your Excel workbook they are not going to be allowed to print it. So I'm going to put cancel equals true then maybe a message box printing is not allowed. So now I'm going to try to print. So I'm going to go to File, and then print. And then I'm going to click here on the Print button here and it says printing is not allowed. And it's not allowed me to print, you can also do something cool with that, which is that you can be selective of who can print your workbook.
So there is actually a function in Excel VBA that is called environment that can determine what the username of the person who's using the computer is. And you can know what the username of yourself is if you're logged on windows with your user. So you can right here in the immediate window, for example environ after the question mark, okay, so you're asking for a certain value and then inside that inside the environment, you're gonna write username share now asking about the current user name that is using the computer Okay, so I got the username, which is myself, you got actually right here. If When user name is not equal to yours, then cancel equals true, else cancel equals false. Okay, and if so now is somebody else other than myself tries to print the workbook is not going to enable him or her to print, but if it is myself and I'm going to be able to print the workbook so that's a bit of security if you want to.
Okay, so that's it for the workbook before print event. Thank you very much guys for watching this video, and I'll see you on the next one.