如何删除MS Word加载项按钮?

时间:2009-05-26 07:10:38

标签: c++ ms-word

我需要以编程方式从MS Word中删除加载项。我删除了与之对应的注册表项,并且该按钮现在已禁用(单击它时没有任何反应),并且加载项不再出现在COM加载项列表中。

但是,该按钮仍保留在加载项功能区菜单中。如何以编程方式删除它?

1 个答案:

答案 0 :(得分:1)

一周后没有答案。你可以告诉它一个懒惰的问题,不是吗?

我目前正在使用CodeProject的解决方案。我的代码似乎有效,但它已经尚未经过测试

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();

希望这可以帮助其他人解决类似问题。