Access Word field under cursor using context menu

Add-in Express™ Support Service
That's what is more important than anything else

Access Word field under cursor using context menu
 
Paul Chen




Posts: 4
Joined: 2012-04-01
I added a context menu to the Word addin Field popup menu. When a field is under cursor, I can click the right mouse button and select the popup menu I added. What I want to do is to access the field under the cursor. I know how to access the previous field and the next field using "WordApp.Selection.PreviousField". But how can I access the field under my cursor? Thanks, Paul
Posted 01 Apr, 2012 16:11:14 Top
Andrei Smolin


Add-in Express team


Posts: 14707
Joined: 2006-05-11
Hi Paul,

You need to check the WordApp.Selection.Fields collection.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 02 Apr, 2012 04:11:38 Top
Paul Chen




Posts: 4
Joined: 2012-04-01
Andrei Smolin, With WordApp.Selection.Fields, how can I refer to the field under cursor? I can use WordApp.Selection.Fields.Item(index) to refer to a field, how do I find the current index number? The context popup menu shows only when my cursor is on top of a field. Thanks, Paul
Posted 02 Apr, 2012 06:37:43 Top
Paul Chen




Posts: 4
Joined: 2012-04-01
Andrei Smolin, I do not select the field. I only put the cursor on top of the field and click. The field becomes grey but not selected (navy blue). So the collection WordApp.Selection.Fields.Count is 0. Thanks, Paul
Posted 02 Apr, 2012 06:54:00 Top
Andrei Smolin


Add-in Express team


Posts: 14707
Joined: 2006-05-11
Hi Paul,

Here's my code:

procedure TAddInModule.adxContextMenu1Controls0Click(Sender: TObject);
var
  range, sel: Word2000.Range;
  fieldToTest: Word2000.Field;
begin
  range := WordApp.Selection.Document.Content;
  sel := WordApp.Selection.Range;
  range.End_ := sel.End_;
  fieldToTest := range.Fields.Item(range.Fields.Count);
  if (sel.InRange(fieldToTest.Result))  then
      ShowMessage('!!! Got It: ' + fieldToTest.Result.Text)
  else
      ShowMessage('!!! Failure!');
end;


Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 02 Apr, 2012 07:45:03 Top
Paul Chen




Posts: 4
Joined: 2012-04-01
Andrei Smolin, Thank you so much for your prompt support. Paul
Posted 02 Apr, 2012 09:43:32 Top
Andrei Smolin


Add-in Express team


Posts: 14707
Joined: 2006-05-11
You are welcome!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 02 Apr, 2012 09:49:57 Top