A lot of successful software has an interface with which other programmers can interact. This type of interface is called API – Application Programming Interface. Whether one piece of software, say Grammarly, can interact with another piece of software, say Microsoft Word, depends on the legal framework of the interface, and whether someone has written that corresponding piece of software that binds the two together.
Regarding Microsoft Word, and most desktop software, the legal situation is that anyone can freely use that API. The benefit for the vendor, like Microsoft, is that the possibility of creating extensions increases the value for the users and at the same time the path dependency of customers on the product.
Cloud APIs are usually not free to use because the computing power used is not that of the user but that of the vendor. For Cloud APIs, the charge is often not for the existence, specification, or implementation of the API, but the computing.
The contract is usually simply in the terms and conditions. In case of Grammarly, I simply do not know whether there is a specific contract in place. In theory, no specific contract is required because the situation should already be fully covered by the EULA of Microsoft Word.
Do you need a specific contract with Microsoft to write your own extension? No. This again should be covered by the EULA.
In some countries, the ability of interacting with an API can be governed by additional laws. For example, in EU countries, you may interact with APIs if it is necessary to ensure interoperability and security of computer systems, even if the actual contract would not allow for doing so.
Disclaimer: I write from logic and experience, but I am not a lawyer, this is not legal advice, and I did not research the EULA of Microsoft Word regarding programming extensions. If you need legally sound advice, ask a lawyer.