2005. január 25., kedd

Creating an Insertable ActiveX control for Microsoft Office


Problem/Question/Abstract:

How to create an ActiveX control for Microsoft Office that is "Insertable"

Answer:

There is a nice feature in Microsoft Word that allows you to put ActiveX controls onto a Word document.  To do so you go to the Insert Menu, and click Object….  This displays a dialog list a number of "Insertable" controls.

There is one problem with this dialog though; there is no browse button and no obvious method of adding your control to the list.  So how do you do it?

Turns out your ActiveX control is missing one registry entry, and the Type Library editor does not give you the option of inserting it.  The entry is “Insertable” (and you’ve been wondering why I have been using that word so much).  The key goes in the HKEY_CLASSES_ROOT section of the registry under a key that is your ActiveX control’s class id.

In the end, your registry should look something like this:

HKEY_CLASSES_ROOT->YourControl.TheClass->Insertable

Now, you can either go into RegEdit and enter this manually (a good way to make sure I’m not lying through my teeth), or you can add this entry automatically when the control is registered.  Ya, I thought so, option number 2 it is:

So, if you want this to be put in the registry automatically...

Go to the unit containing your automation object.
Make sure Registry and Windows are in your uses statement.
Modify your INITIALIZATION section to something like this with a new function:

procedure MoreKeys;
const
  C_KEY: string = 'YourControl.TheClass'; // your controls class ID
var
  oReg: TRegistry;
begin
  oReg := TRegistry.Create;

  try
    oReg.OpenKey(HKEY_CLASSES_ROOT);

    // the true mean create the key if it doesn’t exist
    oReg.OpenKey(C_Key + '\Insertable', True);
  finally
    oReg.CloseKey;
    oReg.Free;
  end;

end;

initialization
  TActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    ...yada, yada, yada);
  MoreKeys;

end.

Nincsenek megjegyzések:

Megjegyzés küldése