tags:

views:

236

answers:

1

I need to programmatically remove an add-in from MS Word. I have deleted the registry entry corresponding to it, and the button is now disabled (nothing happens when you click it) and the add-in no longer appears on the list of COM Add-ins.

The button, however, remains in the Add-ins ribbon menu. How can I remove that programmatically?

+1  A: 

No answers after a week. You can tell its a lazy question, can't you?

I am currently using a solution from CodeProject. My code seems to work, but it has not been tested properly yet.

CoInitialize(NULL);
CLSID clsid;
IDispatch *pWApp, *pCommandBars, *pCommandBar, *pCommandBarControls, *pCommandBarControl;
VARIANT v;
HRESULT hr;

hr = CLSIDFromProgID(L"Word.Application", &clsid);
if (SUCCEEDED(hr))
{

 hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pWApp);
 if (SUCCEEDED(hr))
 {
  VariantInit(&v);
  hr = OLEMethod(DISPATCH_PROPERTYGET, &v, pWApp, L"Commandbars", 0);
  pCommandBars = v.pdispVal;

  VariantInit(&v);
  hr = OLEMethod(DISPATCH_PROPERTYGET, &v, pCommandBars, L"Count", 0);
  INT NumCommBars = v.intVal;

  for (INT i = 1; i <= NumCommBars; i++)
  {
   COleVariant CommandBarIndex((BYTE)i);
   VariantInit(&v);
   hr = OLEMethod(DISPATCH_PROPERTYGET, &v, pCommandBars, L"Item", 1, CommandBarIndex);
   if (FAILED(hr)) continue;
   pCommandBar = v.pdispVal;

   VariantInit(&v);
   hr = OLEMethod(DISPATCH_PROPERTYGET, &v, pCommandBar, L"Controls", 0);
   if (FAILED(hr)) continue;
   pCommandBarControls = v.pdispVal;

   VariantInit(&v);
   hr = OLEMethod(DISPATCH_PROPERTYGET, &v, pCommandBarControls, L"Count", 0);
   if (FAILED(hr)) continue;
   INT NumControls = v.intVal;

   for (INT j = 1; j <= NumControls; j++)
   {
    COleVariant CommandBarControlIndex((BYTE)j);
    VariantInit(&v);
    hr = OLEMethod(DISPATCH_PROPERTYGET, &v, pCommandBarControls, L"Item", 1, CommandBarControlIndex);
    if (FAILED(hr)) continue;
    pCommandBarControl = v.pdispVal;

    VariantInit(&v);
    hr = OLEMethod(DISPATCH_PROPERTYGET, &v, pCommandBarControl, L"Caption", 0);
    if (FAILED(hr)) continue;

    CString ControlCaption(v.bstrVal);

    if (ControlCaption.Find("MyCaption") != -1)
    {
     OLEMethod(DISPATCH_METHOD, NULL, pCommandBarControl, L"Delete", 0);
    }

    pCommandBarControl->Release();
   }

   pCommandBar->Release();
   pCommandBarControls->Release();
  }

  OLEMethod(DISPATCH_METHOD, NULL, pWApp, L"Quit", 0);
  pCommandBars->Release();
  pWApp->Release();
 }
}

CoUninitialize();

Hope this helps others with a similar problem.

Michael Sandler