I would like to know when we can use "Hereby" herein" in a formal writing! Would you give an example please.

It is better not to use such words in formal documents. It is much better to go for a "plain English" style. These days even lawyers are starting to stop using them. They are never necessary and an alternative can always be found, even if it is a bit longer. Here are some examples:

Hereby: by this Deed (or Agreement or as the case may be) In fact "hereby" sometimes does not add anything; "I hereby declare" has the same effect as "I declare".

Herein: in this Deed (etc)

Hereinbefore: above

Hereinafter: below

Hitherto: until now

Henceforth: from the date of the Deed (etc)

If you are going to use such words then you need to understand exactly what they mean and the whole of your document needs to be written in an appropriate style. Using them wrongly or in an otherwise modern style only causes amusement.
 Forbes's reply was promoted to an answer.