|
I honestly don't know. I was debugging the CustomDraw message for some other reason, noticed the uItemState was set and cleared it. At which point I also said "Holy ****".
|
|
|
|
|
Hi all,
i m writing a excel sheet with use os excel automation and i m using this code its forking fine.
but the data alwayz write in sheet one ,i want to write in sheet 2 and also want to rename the sheet.
please tell me how can i do this.
thanks in advance.
HRESULT AutoWrap_Exp(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...)
{
va_list marker;
va_start(marker, cArgs);
if(!pDisp)
{
return 0;
}
DISPPARAMS dp = { NULL, NULL, 0, 0 };
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPID dispID;
HRESULT hr;
char buf[200];
char szName[200];
WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);
hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID);
if(FAILED(hr))
{
sprintf(buf, "IDispatch::GetIDsOfNames(\"%s\") failed w/err 0x%08lx", szName, hr);
OutputDebugStringA(buf);
return hr;
}
VARIANT *pArgs = new VARIANT[cArgs+1];
for(int i=0; i<cArgs; i++)
{
pArgs[i] = va_arg(marker, VARIANT);
}
dp.cArgs = cArgs;
dp.rgvarg = pArgs;
if(autoType & DISPATCH_PROPERTYPUT)
{
dp.cNamedArgs = 1;
dp.rgdispidNamedArgs = &dispidNamed;
}
hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, pvResult, NULL, NULL);
if(FAILED(hr))
{
sprintf(buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx", szName, dispID, hr);
OutputDebugStringA(buf);
return hr;
}
va_end(marker);
delete [] pArgs;
return hr;
}
void CTest_ExcelDlg::OnBnClickedButton2()
{
try
{
CString OpenFile_Name=_T("E:\\xl.xlsx");
CWaitCursor cwt;
DeleteFile(OpenFile_Name);
BOOL flag=FALSE;
int nRow=4;
int nCol=2;
CString locount;
locount.Format(_T("%d"),nRow+1);
CString rowp="";
rowp="A1:B";
rowp+=locount;
CString column="";
CString item="";
CoInitialize(NULL);
CLSID clsid;
HRESULT hr = CLSIDFromProgID(L"Excel.Application", &clsid);
if(FAILED(hr))
{
::MessageBox(NULL, _T("CLSIDFromProgID() failed"), _T("Error"), 0x10010);
return ;
}
IDispatch *pXlApp;
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pXlApp);
if(FAILED(hr))
{
::MessageBox(NULL, _T("Excel not registered properly"), _T("Error"), 0x10010);
return ;
}
{
VARIANT x;
x.vt = VT_I4;
x.lVal = 0;
if(AutoWrap_Exp(DISPATCH_PROPERTYPUT, NULL, pXlApp, L"Visible", 1, x)!=S_OK)
{
flag=TRUE;
goto error;
}
}
IDispatch *pXlBooks;
{
VARIANT result;
VariantInit(&result);
if(AutoWrap_Exp(DISPATCH_PROPERTYGET, &result, pXlApp, L"Workbooks", 0)!=S_OK)
{
flag=TRUE;
goto error;
}
pXlBooks = result.pdispVal;
}
IDispatch *pXlBook;
{
VARIANT result;
VariantInit(&result);
if(AutoWrap_Exp(DISPATCH_PROPERTYGET, &result, pXlBooks, L"Add", 0)!=S_OK)
{
flag=TRUE;
goto error;
}
pXlBook = result.pdispVal;
}
VARIANT arr;
arr.vt = VT_ARRAY | VT_VARIANT ;
{
SAFEARRAYBOUND sab[2];
sab[0].lLbound = 1; sab[0].cElements = nRow+1;
sab[1].lLbound = 1; sab[1].cElements = nCol;
arr.parray = SafeArrayCreate(VT_VARIANT, 2, sab);
}
for(int i=1; i<=nRow+1; i++)
{
for(int j=1; j<=nCol; j++)
{
VARIANT tmp;
if(i==1)
{
if(j==1)
item=_T("Column 1");
else
item=_T("Column 2");
}
else
{
item.Format("Row :: %d and Column :: %d",i-2,j-1);
}
tmp.bstrVal = _bstr_t(item);
tmp.vt = VT_BSTR;
long indices[] = {i,j};
SafeArrayPutElement(arr.parray, indices, (void *)&tmp);
}
}
IDispatch *pXlSheet;
{
VARIANT result;
VariantInit(&result);
if(AutoWrap_Exp(DISPATCH_PROPERTYGET, &result, pXlApp, L"ActiveSheet", 0)!=S_OK)
{
flag=TRUE;
goto error;
}
pXlSheet = result.pdispVal;
}
IDispatch *pXlRange;
{
VARIANT parm;
parm.vt = VT_BSTR;
parm.bstrVal = ::SysAllocString(_bstr_t(rowp));
VARIANT result;
VariantInit(&result);
if(AutoWrap_Exp(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, parm)!=S_OK)
{
flag=TRUE;
goto error;
}
VariantClear(&parm);
pXlRange = result.pdispVal;
}
if(AutoWrap_Exp(DISPATCH_PROPERTYPUT, NULL, pXlRange, L"Value", 1, arr)!=S_OK)
{
flag=TRUE;
goto error;
}
{
VARIANT x;
x.vt = VT_I4;
x.lVal = 1;
if(AutoWrap_Exp(DISPATCH_PROPERTYPUT, NULL, pXlBook, L"Saved", 1, x)!=S_OK)
{
flag=TRUE;
goto error;
}
VARIANT result;
VariantInit(&result);
VARIANT fname;
fname.vt = VT_BSTR;
fname.bstrVal=::SysAllocString(_bstr_t(OpenFile_Name));
if(AutoWrap_Exp(DISPATCH_METHOD, &result, pXlSheet, L"SaveAs", 1, fname)!=S_OK)
{
flag=TRUE;
goto error;
}
}
if(AutoWrap_Exp(DISPATCH_METHOD, NULL, pXlApp, L"Quit", 0)!=S_OK)
{
flag=TRUE;
goto error;
}
error:
if(flag==TRUE)
{
AutoWrap_Exp(DISPATCH_METHOD, NULL, pXlApp, L"Quit", 0);
{
}
pXlRange->Release();
pXlSheet->Release();
pXlBook->Release();
pXlBooks->Release();
pXlApp->Release();
VariantClear(&arr);
CoUninitialize();
MessageBox(_T("Error in file."),_T("Message"),MB_ICONINFORMATION);
return ;
}
pXlRange->Release();
pXlSheet->Release();
pXlBook->Release();
pXlBooks->Release();
pXlApp->Release();
VariantClear(&arr);
CoUninitialize();
}
catch(...)
{
MessageBox(_T("Error in file."),_T("Message"),MB_ICONINFORMATION);
return ;
}
MessageBox(_T("Done."),_T("Message"),MB_ICONINFORMATION);
}
|
|
|
|
|
Instead of getting the "ActiveSheet" of pXLApp, get the "Worksheets" collection of pXLBook, then get "Item" 2 of that collection, that should give you the "sheet 2". Use a propertyput on the "Name" property of the Worksheet to change its name.
|
|
|
|
|
can u please explain it with example.
|
|
|
|
|
I use the Excel Dispatch Wrapper classes, which makes it much easier, this is exactly what I do in my own code
_Workbook m_excel_workbook;
Worksheets m_excel_worksheets;
_Worksheet m_excel_worksheet;
LPDISPATCH pdispatch;
LPCTSTR szSheetName = _T("name of sheet");
COleVariant varIndex((short)2);
pdispatch = m_excel_workbook.GetWorksheets();
if (pdispatch != NULL)
{
m_excel_worksheets.AttachDispatch(pdispatch);
pdispatch = m_excel_worksheets.GetItem(varIndex);
if (pdispatch != NULL)
{
m_excel_worksheet.AttachDispatch(pdispatch);
m_excel_worksheet.SetName(szSheetName);
}
}
Trying to fit that to your code would look something like this, I think
IDispatch* pXlSheets;
VARIANT result;
VariantInit(&result);
AutoWrap_Exp(DISPATCH_PROPERTYGET, &result, pXlBook, L"Worksheets", 0);
pXlSheets = result.pdispVal;
IDispatch* pXlSheet2;
VARIANT item;
result.vt = VT_EMPTY;
result.pdispVal = NULL;
item.vt = VT_I4;
item.lVal = 2;
AutoWrap_Exp(DISPATCH_PROPERTYGET, &result, pXlSheets, L"Item", 1, item);
pXlSheet2 = result.pdispVal;
VARIANT name;
name.vt = VT_BSTR;
name.bstrVal = ::SysAllocString("name of your sheet");
AutoWrap_Exp(DISPATCH_PROPERTYPUT, NULL, pXlSheet2, L"Name", 1, name);
|
|
|
|
|
Thanks its done with your help.
just one more query for this its working fine for default 3 sheets,
how can add new sheet and work for it?
thanks in advance.
modified on Monday, March 7, 2011 11:52 PM
|
|
|
|
|
How I do it :-
COleVariant varOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant varCount((short)(2),VT_I2);
COleVariant varType((short)xlWorksheet,VT_I2);
pdispatch = m_excel_worksheets.Add(varOptional,varOptional,varCount,varType);
pdispatch->Release();
Fitted to your code, ( I think this will work given the quick comparison I did of your AutoWrap_Exp and MFC's InvokeHelper ) :-
IDispatch* pXlSheet2;
VARIANT optional;
result.vt = VT_ERROR;
result.lVal = (long)DISP_E_PARAMNOTFOUND;
VARIANT count;
count.vt = VT_I2;
count.iVal = 2;
VARIANT type;
type.vt = VT_I2;
type.iVal = -4167;
VARIANT result;
VariantInit(&result);
AutoWrap_Exp(DISPATCH_METHOD, &result, pXlSheets, L"Add", 4, optional, optional, count, type);
result.pdispVal->Release();
Access the extra sheets as before using propertyget "Item". Using optional for both Before and After means, if I recall correctly, the 2 new sheets are added to the end of the sheets collection.
|
|
|
|
|
thanks.
if i know how many row write in excel than its working fine and successfully write the excel file.
but when i write excel file from reading a text file so i dont know how many rows are need to be written so i am unable to create this array to assign value.
VARIANT arr;
arr.vt = VT_ARRAY | VT_VARIANT ;
{
SAFEARRAYBOUND sab[2];
sab[0].lLbound = 1; sab[0].cElements = nRow+1;
sab[1].lLbound = 1; sab[1].cElements = nCol;
arr.parray = SafeArrayCreate(VT_VARIANT, 2, sab);
}
please tell me this case how can i write the value in excel file
thanks in advance.
modified on Tuesday, March 8, 2011 3:10 AM
|
|
|
|
|
You could either :-
read the file once, counting the rows, create suitably sized array, read the file again filling in the values
or
read the file once, adding a single row to excel for each row in the file, stopping when there are no more rows in the file
|
|
|
|
|
thanks.
when the data of column is in form of multiline than its automatically display with wrap text option,how can i disable this.
modified on Wednesday, March 9, 2011 5:09 AM
|
|
|
|
|
I have a difficult question. In WM_CHAR: If i use IME: EN, i will receive corect code input char.(a:97). I use IME:Japan, Result is so(corect). When I select IME:VietNamese, Tamil...I received ?(code:63). Please help me.(I set location:japan)
|
|
|
|
|
I just recently tried to create a library and then use the library functions within a simple console application. I followed the instructions at:
http://www.functionx.com/visualc/libraries/staticlib.htm
After copying the "lib" and "h" file into the source file directory for the program, I got a linking error. I added these files to the project via "Add Existing Item" menu selection. The error I got was the following:
A custom build rule to build files with extension 'lib' could not be found.
Would you like to create a new rule to define a custom build rule to build files with this extension?
I would think that using library functions would be a standard functionality for Visual C. Is there a setting somewhere that has to be adjusted to allow for this functionality? If not, does anyone know the rule that needs to be used?
--------------------------------------------------------------------------------
|
|
|
|
|
You don't add the .lib file to the project, since it isn't a source file. Add the filename to the linker options, so it looks there for the library functions that you call.
--Mike--
Dunder-Mifflin, this is Pam.
|
|
|
|
|
Mike,
Thank you for your reply. Where do you add it? I added it to configuration properties\linker\general\additional library directories. I though am now getting LINK2019 for each of the functions. See below:
Exercise.obj : error LNK2019: unresolved external symbol "double __cdecl Max(double const *,int)" (?Max@@YANPBNH@Z) referenced in function _main
1>Exercise.obj : error LNK2019: unresolved external symbol "double __cdecl Min(double const *,int)" (?Min@@YANPBNH@Z) referenced in function _main
1>Exercise.obj : error LNK2019: unresolved external symbol "double __cdecl Average(double const *,int)" (?Average@@YANPBNH@Z) referenced in function _main
1>Exercise.obj : error LNK2019: unresolved external symbol "double __cdecl Sum(double const *,int)" (?Sum@@YANPBNH@Z) referenced in function _main
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
|
|
|
|
|
hello,
in your project properties, linker/input, add your .lib file name.
for eg.
yourlibfile.lib
this will solve your issue.
Regards,
A. Gopinath.
|
|
|
|
|
tagopi,
This is the solution that was posted by WayneAKing in the Visual C++ Developer Center forum. His posting is in response to an error listing that I posted. This guy knows everything. My program is now working.
I added the directory to the project configuration
>properties - linker - general tab.
That's half the requirements.
What did I say to add, and where did I say to add it?
Look under Linker->Input for "Additional Dependencies"
and add your .lib filename to the list.
>int main()
You are making a console program.
>error LNK2019: unresolved external symbol _WinMain@16
>referenced in function ___tmainCRTStartup
WinMain is used with Win32 GUI applications, not console apps.
How did you create the project? Which project template did
you use? You should have selected "Win32 Console Application".
Look under Linker->System for "SubSystem" and set it to
"Console (/SUBSYSTEM:CONSOLE)"
- Wayne
|
|
|
|
|
So here is something really crazy. I have a class, which looks something like this:
protected:
HWND hWindow;
HWND hChildren[100];
private:
public:
Everything works fine. But if i change hChildren[100] to hChildren[1000] - so now i can add up to 1000 children, class starts acting mental. Consider the following:
void function1(){}
void function2(){}
I am calling function1() ,
myClass *cl = new myClass();
cl->function1();
instead of that, the function, which gets called - is function2() . This happens if i change size of hChildren from 100 to 1000. If size is 100, the one which executes - is function1() (which is OK). Have you ever experienced code behavior like this? Its like, some undesirable polymorphism occurred
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
The first thing I do for "crazy, can't happen" stuff is rebuild the whole solution. Oh, and you might try deleting the .ncb file too.
|
|
|
|
|
Although Hans is mosssst likely right (this sort of things happens when you copy some files around and the timestamps of the object in the project are anymore coherent, so you rebuild only partially something that should be rebuilt all)consider if your design needs a review.
An array of 1000 HWND is 4 or KBytes long, and fits all into the object storage that may be used in the stack as a local variable. The space on the stack is not "as wide as the computer is", so think to dynamic allocation for this kind of bulks (for example: std::vector<hwnd> don't have this problem).
Also: a window with 1000 active children is kick in the ass for the operating system: do you really need all of them? Can't you create only the ones that are really visible?
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Emilio Garavaglia wrote: An array of 1000 HWND is 4 or KBytes long, and fits all into the object storage that may be used in the stack as a local variable.
Isn't the stack set to 1 Mb by default?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
It is a parameter defined at link time. The stack is allocated when a thread is created, based on the parameter the shell find in the exe, just before starting the process.
It is 1MB for MS compilers, but other may be different.
But the point is how many of those object can exist on the stack in case of a recursive call: 1MB is not a soooo big space.
If you know that will never happen, OK.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Container of size 1000 never gets really filled, nor does even that one of size 100, so its not that bad, trust me
Basically, i would never go with an app which has 1000 child controls present at a time - it was rather like "testing here, testing there, changing some value here and there, see what can happen", but as well it was a quick check if i can theoretically have an array of 1000 controls. So, thank you for an advice on vector, i haven't considered this, but now will try
Thanks
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
1000 HWNDs on your system? Any idea of the overhead that would place on the Operating System? No wonder things go crazy. I would suggest that even 100 on the average PC is far too many.
I must get a clever new signature for 2011.
|
|
|
|
|
Thanks for you replies. So, rebuilding did not help, deleting ncb file did not help either. Maybe i should use these #pragma pack's ? Anyway, probably i just stick with array of 100 child windows, or will use vector . Well, actually vector is a good idea. These 1000 HWNDs are child windows - controls, so it is not a big deal, they are not like, separated windows (in such case that would be a real overkill), besides, that is just a container so it can hold up to some amount of controls doesnt mean there are that many controls
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
I certainly would not want to use an application that had that many controls on it. As to your problem, are you sure there is not some bug elsewhere in the code?
I must get a clever new signature for 2011.
|
|
|
|
|