Python Getting cursor position in WordApplication with win32

Viewed 235

I would like to add some text after the current position of the cursor in an active word document.

The code I need would have to be something like this:

import win32com.client as win32

word = win32.gencache.EnsureDispatch('Word.Application')
def InsertTextAfterCursor(text,document):
    document.Range(0,document.GETCURSORPOSITION).InsertAfter(text) #GETCURSORPOSITION is what I don't know how to implement
word.Documents().Open("TheDocI'mWorkingOn.docx")    
doc = word.Documents().Item("TheDocI'mWorkingOn.docx")
InsertTextAfterCursor(doc,'The text I want to insert')

The only idea I've come up with is to constantly check for changes of the entire Range of the document, so I can get where I'm writing, but:

  1. this is useless if I just click anywhere in the document without changing anything.
  2. this becomes expensive and slow if I'm dealing with a, say, 200 pages document.

I've been reading the official documentation for a while now but I can't figure this out. Would appreciate any indication

0 Answers
Related