How do I insert text at my cursor location in Word?

13 views
Skip to first unread message

Peerapol Khonarsa

unread,
Oct 29, 2018, 12:14:11 PM10/29/18
to ComfyJ Forum

Example  Word Integration Sample in http://comfyj-support.teamdev.com/samples.  I try to improve program by  insert text at my cursor location in Word. 


How do I insert text at my cursor location in Word?

Natasha Dmitrieva

unread,
Oct 30, 2018, 6:37:55 AM10/30/18
to peerap...@gmail.com, comfyj...@teamdev.com
Hi Peerapol, 
Thank you for your question.

In fact, the approach demonstrated in the sample is based on using the Word Object Model - the same model that is available for VBA macros.
Both the insertion point (text cursor location) and the selection in the Word Object model is represented by the Selection objects. This object contains a Range that can be used in a way that is very similar to the approach demonstrated in the sample.

Here is a sample code that demonstrates how to add some text right after the text cursor:

private void insertTextAtSelection(_DocumentImpl document, String text, int fontSize){ Window activeWindow = document.getActiveWindow(); Selection selection = activeWindow.getSelection(); Range range = selection.getRange(); try { //selection.typeText(new BStr(text)); range.setText(new BStr(text)); _Font font = range.getFont(); try { font.setSize(new SingleFloat(fontSize)); } finally { font.setAutoDelete(false); font.release(); } range.insertParagraphAfter(); } finally { range.setAutoDelete(false); range.release(); selection.setAutoDelete(false); selection.release(); activeWindow.setAutoDelete(false); activeWindow.release(); } }

Regards,
Natasha
Reply all
Reply to author
Forward
0 new messages