You are best off using Word's built-in security rather than trying to write something. However, you can use a
MacroButton Field to run a macro that would simulate a password check. You could store the password as a document variable and close the document if the proper response is not given. This is far from the security offered by Word's password to open or even its password to edit.
You can protect a document by restricting editing and requiring a password to edit protected portions.