Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Problem with Excel's COM interface in XLL

Hi,
I am developing an Excel add-in using XLL (Excel C API). In XLL along
with Excel C API I am using Excel's COM interfaces also. Now I am
stuck in one issue with the COM interfaces. I want to add a menu bar
to the Excel using COM interfaces, I tried the following code but
could not able to add the Menu Bar to the Excel.

Void AddMenuBar()
{

//excelApp is the global variable and is the object of "_Application"
//class. Assume that at this point this variable has been initialized
//with valid value.
LPDISPATCH pCommandBars = excelApp.GetCommandBars();

//After successful addition of menu bar this will have the IDispatch *
//for "MenuBar" class.
VARIANT vtMenuBar;
VariantInit(&vtMenuBar);

//Argument number 1, name of the menubar.
VARIANT vtParam1;
VariantInit(&vtParam1);
vtParam1.vt = VT_BSTR;
vtParam1.bstrVal = _bstr_t("MyComMenu").copy();

//Argument number 2, position of the menubar.
VARIANT vtParam2;
VariantInit(&vtParam2);
vtParam2.vt = VT_INT;
vtParam2.intVal = 2; //add to the right of the doc.

//Argument number 3, Boolean value which specifies whether the command
bar object // is menu bar or not
VARIANT vtParam3;
VariantInit(&vtParam3);
vtParam3.vt = VT_BOOL;
vtParam3.boolVal = VARIANT_TRUE;

//Argument number 4, Boolean value which should be TRUE I guess
VARIANT vtParam4;
VariantInit(&vtParam4);
vtParam4.vt = VT_BOOL;
vtParam4.boolVal = VARIANT_TRUE;

//Number of arguments to the Add method of "CommandBars" class
const int numArgs = 4;

VARIANT vtArg[numArgs];
vtArg[0].vt = VT_VARIANT;
vtArg[0].pvarVal = &vtParam1;

vtArg[1].vt = VT_VARIANT;
vtArg[1].pvarVal = &vtParam2;

vtArg[2].vt = VT_VARIANT;
vtArg[2].pvarVal = &vtParam3;

vtArg[3].vt = VT_VARIANT;
vtArg[3].pvarVal = &vtParam4;

DISPID dwDispID;
DISPPARAMS dispparams = {NULL, NULL, 0, 0};
dispparams.rgvarg = rgVarParams;
dispparams.cArgs = numArgs;

HRESULT hr = pDisp-GetIDsOfNames(IID_NULL, L"Add",
1,LOCALE_USER_DEFAULT,
&dwDispID);

if(SUCCEEDED(hr)) {
hr = pDisp-Invoke(dwDispID, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &dispparams, &vtMenuBar,
NULL, NULL);

if (FAILED(hr)) {
hr = pDisp-Invoke(dwDispID, IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_METHOD| DISPATCH_PROPERTYGET,
&dispparams,
&vtMenuBar, NULL, NULL);
}

}

//Do any clean ups here.

} //AddMenuBar ends here.

Can any one please point out the problem in my code, is there anything
which I am missing or is there any thing which I did in wrong way. Why
the above code fails to add the menu bar to the Excel?

Any help will be much appreciated.

Thanks & Regards,
Mousam Dubey

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Problem with Excel's COM interface in XLL

On Oct 11, 7:30 pm, Mousam wrote:
Hi,
I am developing an Excel add-in using XLL (Excel C API). In XLL along
with Excel C API I am using Excel's COM interfaces also. Now I am
stuck in one issue with the COM interfaces. I want to add a menu bar
to the Excel using COM interfaces, I tried the following code but
could not able to add the Menu Bar to the Excel.

Void AddMenuBar()
{

//excelApp is the global variable and is the object of "_Application"
//class. Assume that at this point this variable has been initialized
//with valid value.
LPDISPATCH pCommandBars = excelApp.GetCommandBars();

//After successful addition of menu bar this will have the IDispatch *
//for "MenuBar" class.
VARIANT vtMenuBar;
VariantInit(&vtMenuBar);

//Argument number 1, name of the menubar.
VARIANT vtParam1;
VariantInit(&vtParam1);
vtParam1.vt = VT_BSTR;
vtParam1.bstrVal = _bstr_t("MyComMenu").copy();

//Argument number 2, position of the menubar.
VARIANT vtParam2;
VariantInit(&vtParam2);
vtParam2.vt = VT_INT;
vtParam2.intVal = 2; //add to the right of the doc.

//Argument number 3, Boolean value which specifies whether the command
bar object // is menu bar or not
VARIANT vtParam3;
VariantInit(&vtParam3);
vtParam3.vt = VT_BOOL;
vtParam3.boolVal = VARIANT_TRUE;

//Argument number 4, Boolean value which should be TRUE I guess
VARIANT vtParam4;
VariantInit(&vtParam4);
vtParam4.vt = VT_BOOL;
vtParam4.boolVal = VARIANT_TRUE;

//Number of arguments to the Add method of "CommandBars" class
const int numArgs = 4;

VARIANT vtArg[numArgs];
vtArg[0].vt = VT_VARIANT;
vtArg[0].pvarVal = &vtParam1;

vtArg[1].vt = VT_VARIANT;
vtArg[1].pvarVal = &vtParam2;

vtArg[2].vt = VT_VARIANT;
vtArg[2].pvarVal = &vtParam3;

vtArg[3].vt = VT_VARIANT;
vtArg[3].pvarVal = &vtParam4;

DISPID dwDispID;
DISPPARAMS dispparams = {NULL, NULL, 0, 0};
dispparams.rgvarg = rgVarParams;
dispparams.cArgs = numArgs;

HRESULT hr = pDisp-GetIDsOfNames(IID_NULL, L"Add",
1,LOCALE_USER_DEFAULT,
&dwDispID);

if(SUCCEEDED(hr)) {
hr = pDisp-Invoke(dwDispID, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &dispparams, &vtMenuBar,
NULL, NULL);

if (FAILED(hr)) {
hr = pDisp-Invoke(dwDispID, IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_METHOD| DISPATCH_PROPERTYGET,
&dispparams,
&vtMenuBar, NULL, NULL);
}

}

//Do any clean ups here.

} //AddMenuBar ends here.

Can any one please point out the problem in my code, is there anything
which I am missing or is there any thing which I did in wrong way. Why
the above code fails to add the menu bar to the Excel?

Any help will be much appreciated.

Thanks & Regards,
Mousam Dubey


Hi,
I found the problem, the solution is as follows.

Following code should be changed as

Problematic code:
VARIANT vtArg[numArgs];
vtArg[0].vt = VT_VARIANT;
vtArg[0].pvarVal = &vtParam1;

vtArg[1].vt = VT_VARIANT;
vtArg[1].pvarVal = &vtParam2;

vtArg[2].vt = VT_VARIANT;
vtArg[2].pvarVal = &vtParam3;

vtArg[3].vt = VT_VARIANT;
vtArg[3].pvarVal = &vtParam4;



Corrected code:

VARIANT vtArg[numArgs];
vtArg[0].vt = VT_BYREF | VT_VARIANT; //VT_VARIANT must be used with
VT_BYREF
vtArg[0].pvarVal = &vtParam4;

vtArg[1].vt = VT_BYREF | VT_VARIANT; //VT_VARIANT must be used with
VT_BYREF
vtArg[1].pvarVal = &vtParam3;

vtArg[2].vt = VT_BYREF | VT_VARIANT; //VT_VARIANT must be used with
VT_BYREF
vtArg[2].pvarVal = &vtParam2;

vtArg[3].vt = VT_BYREF | VT_VARIANT; //VT_VARIANT must be used with
VT_BYREF
vtArg[3].pvarVal = &vtParam1;

Actually arguments in dispparams.rgvarg are stored in reverse order.
Therefore we need to store the first argument at the highest index in
the array.
For details read the help of Invoke function of IDispatch interface.

Thanks & Regards,
Mousam Dubey

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Loading Addins - Excel Started through COM Interface newbie73 Excel Programming 1 September 7th 07 07:30 PM
problem in Excel's VB Help deo89 Excel Discussion (Misc queries) 2 February 22nd 06 10:58 PM
Web Interface Brain Buster Problem (for me at least :) ski Excel Programming 3 December 24th 04 12:57 AM
Web Enabled Excel Interface Problem TerryG Excel Programming 1 October 20th 04 09:12 PM
Tired of Excel's Interface limitations...?? Chris Gorham[_4_] Excel Programming 1 January 9th 04 09:05 PM


All times are GMT +1. The time now is 08:36 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"