Solved

Copying data to clipboard (in Csv format)

Posted on 2002-06-19
4
523 Views
Last Modified: 2013-11-20
Hi,
I'm trying to copy data to clipboard in Csv format.
This is my code:

void CMyDialog1::OnCopy()
{
    COleDataSource* pSource = new COleDataSource();
    UINT aFmt = ::RegisterClipboardFormat(_T("Csv"));
    CSharedFile     sf(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT);
    CString text = _T("45,5,4\n2,1,1");
    sf.Write(text, text.GetLength());
    HGLOBAL hMem = sf.Detach();
    if (!hMem)
     return;
    pSource->CacheGlobalData(aFmt, hMem);
    pSource->SetClipboard();
}

Now, when I'm trying to paste to excel, I do not get 6 cells as I expected (with values: 45 5 4 2 1 1).
What I get is only one cell with the value "4" (which is probably the prefix of "45", because when I replace the "45" in the code to "78" I get one cell with "7").

Can someone please help me with this ?

thanks,
Yoav.



0
Comment
Question by:yoavo
4 Comments
 
LVL 4

Accepted Solution

by:
mblat earned 100 total points
ID: 7093299
The following works for me

    source = "123,25,341,50";
     if(OpenClipboard())
     {
          HGLOBAL clipbuffer;
          char * buffer;
          EmptyClipboard();
          clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
          buffer = (char*)GlobalLock(clipbuffer);
          strcpy(buffer, LPCSTR(source));
          GlobalUnlock(clipbuffer);
          SetClipboardData(CF_TEXT,clipbuffer);
          CloseClipboard();
     }


One note I am using Excel 2000 and to make it work I had to go to Data->Text To Column then Next and make sure that Comma chosen as seprator.

Hope it helps..
0
 

Author Comment

by:yoavo
ID: 7095019
I found my problem. It was because my application is build with UNICODE...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now