Recent

Author Topic: Getting a word out of MS Word  (Read 1120 times)

w click

  • Full Member
  • ***
  • Posts: 137
Getting a word out of MS Word
« 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?

PascalDragon

  • Hero Member
  • *****
  • Posts: 3525
  • Compiler Developer
Re: Getting a word out of MS Word
« Reply #1 on: September 24, 2021, 01:34:07 pm »
Did you try the following?

Code: Pascal  [Select][+][-]
  1. label1.caption := WideString(sourcedoc.WordUnderTheCursor);

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1063
Re: Getting a word out of MS Word
« Reply #2 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
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

w click

  • Full Member
  • ***
  • Posts: 137
Re: Getting a word out of MS Word
« Reply #3 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