I suggest to not do what Felu suggested, at first that is. As this will more than likely lead to errors and possible wrong windows (eg: take in account that there can be more than 1 chat window open, that the first found window is not the top-most chat window, that chat windows can even belong to other Messenger sessions (polygamy), etc etc))
Do as Mattike suggested and add a logical way to retrieve in what chat window you want to write something (by the use of a global variable for example like he suggested).
If this isn't possible at all, then you could do a manual search for chat windows. But use the chatwnds enumeration object and not the findwindow api!!!. And in each enumeration you check the Z-order of the window handle with a windows API, the lowest one (aka the top-most)
, or was it the biggest one.. dunno anymore , is the one you need.
PS: And do not forget to check if you actually can edit/write something to the chat window before you actually write or manipulate the cursor!