Copying data to clipboard to be "Paste" into Excel / word

Hi,

I was hoping someone could help me. I am running a windows C based program and there is data being held in memory. If i hit a buttin on the keyboard i would like to copy all the data to the clipboard and paste it into an Excel spreadsheet.
Thus far I have written the below code.
And while it does not throw any exceptions / return NULL pointers or anythign.. the data is not in the clipboard after i have completed.


///////////////////////////////////////////////////////
typedef struct transtructFormat
{
CHAR ValueDate[10];
CHAR Counterparty[20];
CHAR CcyPair[10];
LONG Amount;
DOUBLE Rate;
CHAR Dealer[5];
CHAR Tp[3];
CHAR Status[4];
LONG DealNumber;
CHAR Med[4];
} TRANSTRUCTFORMAT; // format of the data copied to clipboard



UINT format = RegisterClipboardFormat("TRANSTRUCT_FORMAT");
int l;
HGLOBAL clipbuffer = GlobalAlloc(GMEM_FIXED, sizeof(TRANSTRUCTFORMAT)* TranStruct.lNumEl*/)
TRANSTRUCTFORMAT* pTranstructFormat = (TRANSTRUCTFORMAT*)GlobalLock(clipbuffer);

if (OpenClipboard(hwd) )
{
EmptyClipboard();

for (l = 0; l < tranStruct.lNumEl; ++l)
{
PTRANLISTITEM pItem = TranStruct.Trans[l];

pTranstructFormat[l].Amount = 100;
strcpy(pTranstructFormat[l].CcyPair, "USD/GBP");
strcpy(pTranstructFormat[l].Counterparty, "DEUTSCHE");
strcpy(pTranstructFormat[l].Dealer, "PHAD");
pTranstructFormat[l].DealNumber = 123456;
strcpy(pTranstructFormat[l].Med, "RE2");
pTranstructFormat[l].Rate = 1.06543;
strcpy(pTranstructFormat[l].Status, "VUCK");
strcpy(pTranstructFormat[l].Tp, "E:");
strcpy(pTranstructFormat[l].ValueDate, "2012-01-03");
}


//Put it on the clipboard
GlobalUnlock(clipbuffer);
SetClipboardData(format,clipbuffer);
CloseClipboard();
}
return;



///////////////////////////////////////////////////////////

From above see there is an array of TranStruct.Trans and i want to copy data from each one into an array of (dynamically created) TRANSTRUCTFORMAT
and then copy to the clipboard... to then paste into excel.

Can anyone assist please
thank you
Well, aside from the ordering of your calls (why GlocalAlloc if the call to OpenClipboard has failed, etc), MSDN does state that memory must be allocated (with GlobalAlloc) as GMEM_MOVEABLE

If the hMem parameter identifies a memory object, the object must have been allocated using the function with the GMEM_MOVEABLE flag.


whereas you're using GMEM_FIXED.

Andy
Hi andy,

I get the same error.
the clipboard viewer is moaning that it cannot display the informatin in its current format.

which has essentially been the problem the whole way along.
I'm now sure why the data is going missing.

But Excel won't know anything about your custom format. You need to provide data in one of the formats that Excel can already handle, or implement an OLE DataObject which can convert the data into one of the formats that Excel knows about on request.
Topic archived. No new replies allowed.