Developing smart tags for Excel,
Word, PowerPoint and Outlook in Delphi

Add-in Express™
for Microsoft® Office and Delphi® VCL

Add-in Express Home > Add-in Express for Office and Delphi VCL > Online Guide > Writing smart tags

Developing Office smart tags

This sample project implementing a smart tag is included in Add-in Express for Office and VCL sample projects available on the Download page. You can find many other examples on the How-To pages: Delphi for Outlook and Delphi for Excel.

Step 1. Creating a new smart tag library project

Make sure that you have administrative permissions before running Delphi. Also, if you have Windows Vista, Windows 7, Windows Server 2008 or Windows 8, run Delphi via Run as Administrator.

Add-in Express adds the Smart Tag project template to the New Items dialog:

Creating a new smart tag project

When you select the template and click OK, the Smart Tag project wizard starts. In the wizard windows, you choose the project options. The project wizard creates and opens the Smart Tag project in the Delphi IDE.

The smart tag project in Delphi

The smart tag project includes the following items:

  • The project source files (MySmartTag1.*);
  • The type library files (MySmartTag1.ridl and MySmartTag1_TLB.pas);
  • The smart tag module (MySmartTag1_IMPL.pas and MySmartTag1_IMPL.dfm) discussed in the following step.

Step 2. Smart Tag module

The smart tag module (MySmartTag1_IMPL.pas and MySmartTag1_IMPL.dfm) is the core part of the smart tag project. The smart tag module is a container for TadxSmartTag components. The code for MySmartTag1_IMPL.pas is as follows:


unit MyRtdServer1_IMPL;

interface

uses
  SysUtils, Classes, ComServ, MyRtdServer1_TLB, adxRTDServ;

type
  TcoMyRtdServer1 = class(TadxRTDServer, IcoMyRtdServer1);

  TRTDServerModule = class(TadxXLRTDServerModule)
  private
  protected
  public
  end;

implementation

{$R *.dfm}

initialization
  TadxRTDFactory.Create(ComServer, TcoMyRtdServer1,
	CLASS_coMyRtdServer1, TRTDServerModule);

end.

The smart tag module contains three classes:

  • The "interfaced" classes (TcoMySmartTag1Recognizer and TMySmartTag1Action)
  • The smart tag module class (TSmartTagModule).

The "interfaced" classes are descendants of the TadxRecognizerObject class and the TadxActionObject class that implement the smart tag specific interfaces required by the smart tag architecture: ISmartTagRecognizer, ISmartTagRecognizer2, ISmartTagAction and ISmartTagAction2. Usually, you don't need to change anything in these classes.

In the smart tag module class, we write the functionality to be implemented by the smart tag. The smart tag module is an analogue of the Data Module, but unlike the Data Module, the smart tag module allows you to set all properties of your smart tags. This is exactly what we are going to do now.

Step 3. Smart Tag designer

In the Project Manager window, select the smart tag module, activate the Object Inspector window, specify your smart tag name in the SmartTagName property (this name appears in the Smart Tags tab on the host application AutoCorrect Options dialog box), and enter the description of the smart tag through the SmartTagDesc property. These properties depend on Office localization.

Smart tag module properties

The designer of the Smart Tag Module allows setting smart tag library properties and adding TadxSmartTag components to the module.

Step 4. Adding a new smart tag

To add a new Smart Tag to your library, select the Add-in Express tab on the Tool Palette, find the TadxSmartTag component and drag-n-drop it onto the Smart Tag module.

Smart Tag component

In the Object Inspector window, specify the caption for the added smart tag. The value of the Caption property will become a caption of the smart tag context menu. Also, specify the phrase(s) recognizable by the smart tag in the RecognizedWords string collection.

Smart tag component properties

Say, in this sample Smart Tag, the words are as follows:

The editor of the RecognizedWords property

Step 5. Adding and handling Smart Tag actions

To add a new smart tag action, right-click the smart tag component, select the Smart Tag Actions item on the pop-up menu, and, in the Editing window, click the Add New button. In the Editing window, select the action, activate the Object Inspector window, and fill in the Caption property. It depends on Locale. The value of the Caption property is shown as an item of the smart tag context menu (pop-up).

Smart tag action properties

To handle the click event of this menu item, select the Events tab of the Object Inspector, double click the OnClick event, and enter your code:


procedure TSmartTagModule.adxSmartTag1Actions0Click(Sender: TObject;
  const AppName: WideString; const Target: IDispatch; const Text,
  Xml: WideString; LocaleID: Integer);
begin
  ShowMessage('Recognized text is ' + Text);
end;

Step 6. Running your smart tag

Choose Register ActiveX Server in menu Run, restart Word or Excel, enter the words recognizable by your smart tag into a document, and see if the smart tag works.

  • In Office 2003 - 2003, choose the Tools | AutoCorrect menu item and find your smart tag on the Smart Tags tab.
  • In Office 2007, the path to this dialog is as follows: Office button | Word Options | Add-ins | Manage Smart Tags | Go.
  • In Office 2010 and 2013, see File tab | Options | Add-ins | "Manage" Actions | Go.

Autocorrect options dialog

Step 7. Debugging the smart tag

To debug your Smart Tag, just indicate the add-in host application as the Start Program in the Project Options window.

Debugging the smart tag

Step 8. Deploying the smart tag

Make sure your setup project registers the smart tag DLL. Say, in Inno Setup projects you use the 'regserver' command.

Developing Excel RTD servers <<

>> Excel Automation add-ins