Reading "Higlighted Text" in all MS Office Products

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

Reading "Higlighted Text" in all MS Office Products
 
George Spears




Posts: 79
Joined: 2010-05-06
Hello,

I am looking for a way to create a plug-in that will work with all 4 major MS-Office products (Word, Outlook, Excel, and PowerPoint). When the user has highlighted a portion of text (which will be a business name), and right clicks to get the menu, I will have a menu option to provide additional information about that business. The question is: Is there a common way to read/identify/find the text that is highlighted in all 4 products?

Thanks
George S
Posted 17 Jun, 2015 12:37:35 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello George,

I assume you talk about the text selected, not highlighted. In Word-Excel 2002-2007, PowerPoint 2003-2007, and Word editor in Outlook 2002-2007, you can use smart tags. Starting from Office 2010, smart tags are deprecated and you need to add a button to [almost] all context menus. To modify the button's properties depending on the selection (say to show/hide the button), you will need to use the PropertyChanging event of the button as described in section Updating Ribbon Controls at Run-Time of the manual, see the PDF file in the folder {Add-in Express}\Docs on your development PC. To find out the text to check, you will need to parse the current selection.


Andrei Smolin
Add-in Express Team Leader
Posted 18 Jun, 2015 07:20:12 Top
George Spears




Posts: 79
Joined: 2010-05-06
Thank you. Let me drill down a little more. In Outlook 2010 (or 2013), assume someone has sent me an email. I go to the body of the email, and select a phrase. At this point, I can hit Ctrl-C and copy the phrase. Instead, I want to hit a button (or context menu) and be able to do something with that phrase. How do I read the selected text? In Word, it would be


WordApp.Connect;
MySelectionText := WordApp.Selection.Text;


but I can't find anything similar in Outlook. I also tried the following with inconsistent results (it caused issues during debugging).


var
  Buff: array [0 .. 65535] of char;
..
function TAddInModule.CurrentSelectedText: string;
var
  hFocus: hWnd;
  aStart, aEnd: Integer;
begin
  AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), True);
  hFocus := GetFocus;
  SendMessage(hFocus, WM_GETTEXT, 65535, Integer(@Buff));
  SendMessage(hFocus, EM_GETSEL, Integer(@aStart), Integer(@aEnd));
  result := Copy(StrPas(Buff), 1 + aStart, aEnd - aStart);
end;


Thanks
Posted 02 Jul, 2015 07:44:31 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
George,

Inspector.WordEditor returns a Document object from the Word object model. You call Document.Application.Selection and parse the selection.


Andrei Smolin
Add-in Express Team Leader
Posted 06 Jul, 2015 06:55:24 Top
George Spears




Posts: 79
Joined: 2010-05-06
Thank you. For Outlook 2013, this works great IF I am responding to an email, and I have the response in the 'Popout' window. If I have selected text in an email in the preview pane, it doesn't work. How do I get access to the Doc object model when the email is in the preview pane? Here is the code I am using now...


 var
  Doc: OleVariant;

begin

OutlookApp.Connect;

try
  Doc := OutlookApp.ActiveInspector.WordEditor as _Document;
  ShowMessage(Doc.Application.Selection.Text);
except
  ShowMessage('Not Valid');
end;
Posted 06 Jul, 2015 20:20:38 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
George,

In this case, you use the Outlookapp.ActiveInlineResponseWordEditor property. The property was introduced in Outlook 2013.


Andrei Smolin
Add-in Express Team Leader
Posted 07 Jul, 2015 04:17:58 Top
George Spears




Posts: 79
Joined: 2010-05-06
Andrei,

Thank you for your patience here. ActiveInlineResponseWordEditor appears to be a property of Explorer. So

OutlookApp.ActiveExplorer.ActiveInlineResponseWordEditor...


should give me what I need. The issue is that this is not a valid property. You said it was introduced in Outlook 2013, so I imported Outlook 15 Object library (OUTLOOK_TLB) and removed Outlook2000. Not only does that NOT provide the ActiveInlineResponseWordProperty, I now get "Incompatible types: Outlook_TLB._Inspector and Outlook2000._Inspector".

There is a dependency issue here I am not understanding... What do I need in my USES clause to make AddIn Express work with ActiveInlineResponseWordEditor?

Thanks
Posted 07 Jul, 2015 08:18:14 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello George,

You need to use Outlook2000. Import the Outlook 2013 type library (e.g. Outlook_TLB), make sure all Outlook-related types refer to Outlook2000 e.g. Outlook2000.MAPIFolder. When required, cast TadxAddinModule.OutlookApp.DefaultInterface to Outlook_TLB.OutlookApplication and call Outlook_TLB.OutlookApplication.ActiveExplorer.ActiveInlineResponse.


Andrei Smolin
Add-in Express Team Leader
Posted 08 Jul, 2015 05:47:00 Top
George Spears




Posts: 79
Joined: 2010-05-06
Andrei, Thanks for your patience, but I just can't seem to get this to work... Outlook 2013, Delphi XE6, all 32 bit.


uses
  SysUtils, ComObj, ComServ, ActiveX, Variants,  Office2000,  Outlook_TLB, Outlook2000, Word2000, adxAddIn, ....


procedure TAddInModule.ShowHighLightTextButton(Sender: TObject;
  const RibbonControl: IRibbonControl);
  var  
  Doc: OleVariant;
  
begin
OutlookApp.Connect;

try

Doc := (OutlookApp.DefaultInterface as Outlook_TLB.OutlookApplication).ActiveExplorer.ActiveInlineResponse as _Document;
ShowMessage(Doc.Application.Selection.Text);
except
 ShowMessage('Not Valid');
end;


I bring up Outlook, click on an email, highlight a portion of text in the preview pane, then run my code above...

Everything compiled fine, but at runtime, (OutlookApp.DefaultInterface as Outlook_TLB.OutlookApplication).ActiveExplorer is nil, so I get an AV.

What I am not understanding here?
Again, thanks for your patience.

George S
Posted 08 Jul, 2015 19:16:51 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello George,

George Spears writes:
at runtime, (OutlookApp.DefaultInterface as Outlook_TLB.OutlookApplication).ActiveExplorer is nil


I suppose you get nil after calling (OutlookApp.DefaultInterface as Outlook_TLB.OutlookApplication).ActiveExplorer.ActiveInlineResponse. I think so because the steps above don't include clicking Reply before you select text in the Preview Pane.


Andrei Smolin
Add-in Express Team Leader
Posted 09 Jul, 2015 02:33:42 Top