Click to See Complete Forum and Search --> : String from C++ code pasted into Excel has formatting problem


0xC0000005
April 17th, 2008, 11:39 AM
TCHAR text[256];
_stprintf(text, _T("2-4"));

Using code like this I copy 'text' to the clipboard and then paste it into Excel. But Excel interprets "2-4" as February 4th and puts "4-Feb" into the cell.

Is there some way I can format a string so that Excel does not try to do a conversion?

When entering the string directly into Excel typing an apostrophe (') first will work, but from C++ code the apostrophe stays when pasted into the cell.

I have also tried putting quotes around the string - _stprintf(_T("\"2-4\"")) - but that doesn't work either.

Plasmator
April 17th, 2008, 11:55 AM
I'm no expert, but if you're doing this manually, why don't you turn off formatting for the cells that you're going to be pasting data into? Screen-shot attached.

Edit: I think I misread your post. :eek:
Edit2: This seems to work:

="2-4"
="2-5"
="2-6"

0xC0000005
April 17th, 2008, 12:14 PM
Thanks for the suggestion, but there are several reasons why I want to avoid a solution like that.

First, the data that will be copied is primarily numbers so having them also formatted as text in the spreadsheet will cause another set of problems.

Second, I don't like the idea of telling all of our customers that they have to format the spreadsheet first. From a user's point of view the copy and paste should just work. That's why I am looking for a solution that tags a cell as text at the source (the C++ code).

0xC0000005
April 17th, 2008, 12:17 PM
Thanks,

="2-4" does work.