Lazarus

Programming => Operating Systems => Windows => Topic started by: w click on September 24, 2021, 12:37:31 pm

Title: Getting a word out of MS Word
Post by: w click on September 24, 2021, 12:37:31 pm
I'm trying to pass the word under the cursor from Microsoft Word to a program.

I'm using olevariants and gone with the following:-
   WordApp:=CreateOleObject('Word.Application');
   sourcedoc:=wordapp.Documents.Open(WideString(utf8decode(filename)));

And Word opens up fine.  Now I want to do something like:-
   label1.caption:=sourcedoc.WordUnderTheCursor;

I've tried all sorts of things, but I've simply not hit upon the magic word.  I am getting 'Incompatible type for arg no. 1: Got "OleVariant", expected "TTranslateString" when I tried 'sourcedoc.selection.text' and other wild experiments.

Can anyone help, please?
Title: Re: Getting a word out of MS Word
Post by: PascalDragon on September 24, 2021, 01:34:07 pm
Did you try the following?

Code: Pascal  [Select][+][-]
  1. label1.caption := WideString(sourcedoc.WordUnderTheCursor);
Title: Re: Getting a word out of MS Word
Post by: Jurassic Pork on September 24, 2021, 02:26:55 pm
hello,
WordUnderTheCursor does'nt exist and Selection applies to application not document. Try :
Code: Pascal  [Select][+][-]
  1. label1.caption := WideString(WordApp.Selection.Text);

Friendly, J.P
Title: Re: Getting a word out of MS Word
Post by: w click on September 24, 2021, 03:11:30 pm
Pascal Dragon: Yep, tried that ;-)
Jurassic Pork: that does the trick - thanks.

Except to be really clever I need to highlight the word.  I have to double click the word, otherwise I just get the letter the cursor is on.  Do you know the magic code to do an automatic double click, word highlight or whatever?
TinyPortal © 2005-2018