CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Posts
    1,435

    String from C++ code pasted into Excel has formatting problem

    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.

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: String from C++ code pasted into Excel has formatting problem

    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.
    Edit2: This seems to work:
    ="2-4"
    ="2-5"
    ="2-6"
    Attached Images Attached Images
    Last edited by Plasmator; April 17th, 2008 at 12:13 PM.

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: String from C++ code pasted into Excel has formatting problem

    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).

  4. #4
    Join Date
    May 2002
    Posts
    1,435

    Re: String from C++ code pasted into Excel has formatting problem

    Thanks,

    ="2-4" does work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured