Author Topic: COM Client Test Application - For MS Excel, MS Word  (Read 1235 times)

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 69
COM Client Test Application - For MS Excel, MS Word
« on: December 14, 2010, 06:26:08 pm »
Here is the block of code that can create MS Word Application and Invoke and sets its property Visible to true using IDispatch Interface

{
    ::CoInitialize(NULL);
    IDispatch *worddispatch = NULL;

    if( worddispatch == NULL)
    {
        CLSID clsid;
        CLSIDFromProgID(L"Word.Application", &clsid);
        HRESULT hRes = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
                           IID_IDispatch, (void**)&worddispatch);
        if(FAILED(hRes))
        {
            MessageBox("Failed to create MS Word Application Object");
            return;
        }

        DISPID dispidVisible;
        OLECHAR *propertyName = L"Visible";
        hRes = worddispatch->GetIDsOfNames(IID_NULL, &propertyName, 1, LOCALE_SYSTEM_DEFAULT, &dispidVisible);
         
        if(FAILED(hRes))
        {
            MessageBox("Failed to access MS Word Application Methods");
            return;
        }
         
        unsigned returnArg;
        VARIANT varTrue;
        DISPID rgDispidNamedArgs[1];
        rgDispidNamedArgs[0] = DISPID_PROPERTYPUT;
        DISPPARAMS params = { &varTrue, rgDispidNamedArgs, 1, 1 };

        varTrue.vt = VT_BOOL;
        varTrue.boolVal = 0xFFFF;
         
        hRes = worddispatch->Invoke(dispidVisible, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &params, 0, 0, &returnArg);
         
        if(FAILED(hRes))
        {
            MessageBox("Failed to access MS Word Application Methods");
            return;
        }
       
        worddispatch->Release();
    }



Here is the block of code that can create MS Excel Application and Invoke and sets its property Visible to true using IDispatch Interface
{
    ::CoInitialize(NULL);

    if( exceldispatch == NULL)
    {
        CLSID clsid;
        CLSIDFromProgID(L"Excel.Application", &clsid);
        HRESULT hRes = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
                           IID_IDispatch, (void**)&exceldispatch);
        if(FAILED(hRes))
        {
            MessageBox("Failed to create Excel Application Object");
            return;
        }

        DISPID dispidVisible;
        OLECHAR *propertyName = L"Visible";
        hRes = exceldispatch->GetIDsOfNames(IID_NULL, &propertyName, 1, LOCALE_SYSTEM_DEFAULT, &dispidVisible);
         
        if(FAILED(hRes))
        {
            MessageBox("Failed to access Excel Application Methods");
            return;
        }
         
        unsigned returnArg;
        VARIANT varTrue;
        DISPID rgDispidNamedArgs[1];
        rgDispidNamedArgs[0] = DISPID_PROPERTYPUT;
        DISPPARAMS params = { &varTrue, rgDispidNamedArgs, 1, 1 };

        varTrue.vt = VT_BOOL;
        varTrue.boolVal = 0xFFFF;
         
        hRes = exceldispatch->Invoke(dispidVisible, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &params, 0, 0, &returnArg);
         
        if(FAILED(hRes))
        {
            MessageBox("Failed to access Excel Application Methods");
            return;
        }
       
        // do not release now otherwise it will close the excel application
        //exceldispatch->Release();
    }
}

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1